XML to DataSets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lgbjr

    #1

    XML to DataSets

    Hi All,

    I've been given an XML file (12 MB) that I need to extract data from. The
    XML file makes the data easy to view, but the data is impossible to analyze
    in its current format. I've looked at lots of XMLTextReader stuff and I
    can't seem to figure out how to extract the info that I want. Here's a quick
    example of the XML format:

    <instance identity="A1" name="A1" type="A_Type">
    <attribute Name="AttA1-1">
    <value>
    X
    </value>
    </attribute>
    <instance identity="B1" name="B1" type="B_Type">
    <attribute Name="AttB1-1">
    <value>
    X
    </value>
    </attribute>
    <instance identity="B2" name="B2" type="B_Type">
    <attribute Name="AttB2-1">
    <value>
    X
    </value>
    </attribute>
    <instance identity="C1" name="C1" type="C_Type">
    <attribute Name="AttC1-1">
    <value>
    X
    </value>
    </attribute>
    </instance>
    </instance>
    </instance>
    </instance>

    Each A_Type contains several B_Types, and each B_Type contains several
    C_Types and the file contains lots of A_Types. I've shown one attribute
    each, but there are lots of them for each type!

    what I'd like to do is load this data into a few tables with Master-Detail
    Releations. So Table A would contain a row for each A_Type plus it's
    attributes (with the Attribute Name as the Column Header). For a given row
    selected in Table A, Table B would show each of the B_Types and it's
    attributes, etc.

    So, is there an easy way to get the data from the file using an
    XMLTextReader or some other nifty XML tool, or is this going to be a brute
    force method with regular expressions and do loops?

    I'm not sure if I've supplied enough info, so if you need to know something
    else before you can give me an answer, please let me know!! If it's going to
    be brute force, that's fine, but I'd hate to spend a few days coding, only
    later to find out that there was a quick way to do this!

    TIA
    Lee


  • Cor Ligthert

    #2
    Re: XML to DataSets

    lgbjr,

    You can always try to open it as file in your IDE, than you get than some
    information.

    Cor


    Comment

    • lgbjr

      #3
      Re: XML to DataSets

      Cor,

      this I understand. I can open the document in IE, I can import it into
      Excel, but in both cases, the format is nice for viewing, but not for data
      analysis. the way the data is presented is tabular, rather than columnar.
      So, If I want to look at a particular attribute for each Type_C, I have lots
      of scrolling up and down to compare the values. Understand?

      what I need is to convert the XML file to a dataset, with a table for each
      Type, columns for each atrribute in the Type, and rows for each instance of
      the Type.

      I have found lots of information for creating an XML document from an
      existing Dataset, but have not had any luck finding anything regarding
      creating a dataset from an existing XML document. I know I can do this
      manually (going through the file, finding all of the Types (tables), and
      attributes (columns), but I was hoping that there was a tool available that
      would do this for me (Input to the tool would be the XML document, the
      output would be a dataset) Or I'd be happy with an MDB file or SQL DB,
      though I really only need the dataset since the data is already stored on
      disk in the XML file (No need to store the data in 2 locations!)

      Once I have the dataset, I can "Load" the XML document into the dataset and
      do whatever analysis that is required. What I'd like to be able to do is to
      dynamically generate the dataset (Step 1 - read the XML file and generate
      the dataset, Step 2 - load the data from the XML file into the dataset). Is
      there a tool that can do this. Can VB.NET do this? Or do I have to manually
      create the dataset first, then load the data from the XML file?

      TIA
      Lee
      "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
      news:uTSGFjTWFH A.3280@TK2MSFTN GP09.phx.gbl...[color=blue]
      > lgbjr,
      >
      > You can always try to open it as file in your IDE, than you get than some
      > information.
      >
      > Cor
      >[/color]


      Comment

      • Cor Ligthert

        #4
        Re: XML to DataSets

        lgbjr,

        Did you do in your solution explorere: add existing item, choose XML , and
        than browse to your XLM file?

        Cor


        Comment

        • lgbjr

          #5
          Re: XML to DataSets

          Cor,

          Sure, that's not a problem. I can open the XML file in the viewer and what I
          see (because I can't use an XSLT file) is the XML code similar to what I
          type in my first post.

          But, unfortunately, this doesn't get me any closer to getting the data from
          the XML file into a dataset.

          cheers
          Lee

          "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
          news:OBSZBJUWFH A.1404@TK2MSFTN GP09.phx.gbl...[color=blue]
          > lgbjr,
          >
          > Did you do in your solution explorere: add existing item, choose XML , and
          > than browse to your XLM file?
          >
          > Cor
          >[/color]


          Comment

          • Doug Taylor

            #6
            Re: XML to DataSets

            On Sun, 15 May 2005 21:44:58 +0800, "lgbjr" <lgbjr@online.n ospam>
            wrote:
            [color=blue]
            >Cor,
            >
            >Sure, that's not a problem. I can open the XML file in the viewer and what I
            >see (because I can't use an XSLT file) is the XML code similar to what I
            >type in my first post.
            >
            >But, unfortunately, this doesn't get me any closer to getting the data from
            >the XML file into a dataset.
            >[/color]

            Once opened in the explorer if it is well formed XML, you should be
            able to right click and get it to to create a schema for the XML, once
            you have a schema, (may be label view data) it is easy to read it in
            to a dataset.

            Doug Taylor[color=blue]
            >cheers
            >Lee
            >
            >"Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
            >news:OBSZBJUWF HA.1404@TK2MSFT NGP09.phx.gbl.. .[color=green]
            >> lgbjr,
            >>
            >> Did you do in your solution explorere: add existing item, choose XML , and
            >> than browse to your XLM file?
            >>
            >> Cor
            >>[/color]
            >[/color]

            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: XML to DataSets

              lgbjr,
              I would consider using an XSLT transform to transform your input data into
              something a little more DataSet friendly. Here is the start of such a
              transform:

              <?xml version="1.0" encoding="UTF-8" ?>
              <xsl:styleshe et version="1.0"
              xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
              <!-- handle the document/root node of the input -->
              <xsl:template match="/">
              <xsl:element name="lbgjr">
              <xsl:apply-templates select="*" />
              </xsl:element>
              </xsl:template>
              <!-- convert each instance node into a node that is the same as the Type
              attribute -->
              <!-- from: -->
              <!-- <instance identity="A1" name="A1" type="A_Type"> -->
              <!-- <instance identity="B1" name="B1" type="B_Type"> -->
              <!-- <instance identity="C1" name="C1" type="C_Type"> -->
              <!-- to: -->
              <!-- <A_Type identity="A1" name="A1" type="A_Type"> -->
              <!-- <B_Type identity="B1" name="B1" type="B_Type"> -->
              <!-- <C_Type identity="C1" name="C1" type="C_Type"> -->
              <xsl:template match="instance ">
              <xsl:element name="{@type}">
              <xsl:for-each select="@*">
              <xsl:attribut e name="{local-name(.)}">
              <xsl:value-of select="." />
              </xsl:attribute>
              </xsl:for-each>
              <xsl:apply-templates select="*" />
              </xsl:element>
              </xsl:template>
              <!-- convert each Attribute node into a node that has the same name as the
              attribute -->
              <!-- from: -->
              <!-- <attribute Name="AttA1-1"> -->
              <!-- <value>X</value> -->
              <!-- </attribute> -->
              <!-- to: -->
              <!-- <AttA1-1>X</AttA1-1> -->
              <xsl:template match="attribut e">
              <xsl:element name="{@Name}">
              <xsl:value-of select="value" />
              </xsl:element>
              </xsl:template>
              </xsl:stylesheet>

              You can call it with code similar to:

              Dim xslt As New XslTransform
              xslt.Load("lgbj r.xslt")
              xslt.Transform( "lgbjr.xml" , "lgbjr.output.x ml", Nothing)
              Dim ds As New DataSet
              ds.ReadXml("lgb jr.output.xml")

              I've only minimally tested the above template, as your sample data is not as
              well formed as the description you gave...

              Hope this helps
              Jay

              "lgbjr" <lgbjr@online.n ospam> wrote in message
              news:umq%23sSTW FHA.2540@tk2msf tngp13.phx.gbl. ..
              | Hi All,
              |
              | I've been given an XML file (12 MB) that I need to extract data from. The
              | XML file makes the data easy to view, but the data is impossible to
              analyze
              | in its current format. I've looked at lots of XMLTextReader stuff and I
              | can't seem to figure out how to extract the info that I want. Here's a
              quick
              | example of the XML format:
              |
              | <instance identity="A1" name="A1" type="A_Type">
              | <attribute Name="AttA1-1">
              | <value>
              | X
              | </value>
              | </attribute>
              | <instance identity="B1" name="B1" type="B_Type">
              | <attribute Name="AttB1-1">
              | <value>
              | X
              | </value>
              | </attribute>
              | <instance identity="B2" name="B2" type="B_Type">
              | <attribute Name="AttB2-1">
              | <value>
              | X
              | </value>
              | </attribute>
              | <instance identity="C1" name="C1" type="C_Type">
              | <attribute Name="AttC1-1">
              | <value>
              | X
              | </value>
              | </attribute>
              | </instance>
              | </instance>
              | </instance>
              | </instance>
              |
              | Each A_Type contains several B_Types, and each B_Type contains several
              | C_Types and the file contains lots of A_Types. I've shown one attribute
              | each, but there are lots of them for each type!
              |
              | what I'd like to do is load this data into a few tables with Master-Detail
              | Releations. So Table A would contain a row for each A_Type plus it's
              | attributes (with the Attribute Name as the Column Header). For a given row
              | selected in Table A, Table B would show each of the B_Types and it's
              | attributes, etc.
              |
              | So, is there an easy way to get the data from the file using an
              | XMLTextReader or some other nifty XML tool, or is this going to be a brute
              | force method with regular expressions and do loops?
              |
              | I'm not sure if I've supplied enough info, so if you need to know
              something
              | else before you can give me an answer, please let me know!! If it's going
              to
              | be brute force, that's fine, but I'd hate to spend a few days coding, only
              | later to find out that there was a quick way to do this!
              |
              | TIA
              | Lee
              |
              |


              Comment

              • lgbjr

                #8
                Re: XML to DataSets

                Hi All,

                OK, I'm starting to understand a bit of what I need to do (and why). When I
                add the XML file to the project, open it, then right click in the view and
                select View Data Grid, I get an error that says "The Table (instance) cannot
                be the child table to itself in nested relations. this makes sense based on
                the way the XML file is setup.

                So, I need to do as Jay recommended, which is to transform each instance to
                its type and each attribute to its name. Then, each instance will be a table
                (multiples of the same instance will be rows in the table) and each
                attribute in an instance will be a column in the table.

                So, I'm working with Jay's XSLT file as a start and am using the bit of code
                that he supplied to load the XSLT file, then do the transform.

                Here's the first problem. when I run the transform, I get an exception:

                The empty string '' is not a valid name.

                And the output XML file is obviously incomplete.

                This is the top of the XML file that I want to transform:

                <?xml version="1.0" ?>
                <top>
                <model mim="CO_BSSMIM" name="MIB_BSS_B SS_TaiZhouBSS01 _13314645990348 0000"
                prefixDN="RMA=R adio_Access_Net work" release="UNDEFI NED" rootLDN=""
                version="2.2">
                <instance identity="insta nce_1151" name="BSS_TaiZh ouBSS01" type="BSS">
                <attribute name="BSSId">
                <datatype>
                <string>
                <value>
                BSS_TaiZhouBSS0 1
                </value>
                </string>
                </datatype>
                </attribute> <instance identity="insta nce_0" name="RBS_H101"
                type="RBS">
                <attribute name="RBSId">
                <datatype>
                <string>
                <value>
                RBS_H101
                </value>
                </string>
                </datatype>
                </attribute> <attribute name="RBSModel" >
                <datatype>
                <enumRef name="RBSModelT ype">
                <value>
                1
                </value>
                </enumRef>
                </datatype>
                </attribute> <attribute name="SectorCon figuration">
                <datatype>
                <enumRef name="SectorCon figurationType" >
                <value>
                0
                </value>
                </enumRef>
                </datatype>
                </attribute>

                this is the output XML file:

                <?xml version="1.0" encoding="utf-8"?><lgbjr>

                <BSS

                There are no trailing spaces in the input XML file. The transform is failing
                at the first transformation. Can someone tell me why?

                thanks!!

                Lee

                "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                news:evJxLzZWFH A.1404@TK2MSFTN GP09.phx.gbl...[color=blue]
                > lgbjr,
                > I would consider using an XSLT transform to transform your input data into
                > something a little more DataSet friendly. Here is the start of such a
                > transform:
                >
                > <?xml version="1.0" encoding="UTF-8" ?>
                > <xsl:styleshe et version="1.0"
                > xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
                > <!-- handle the document/root node of the input -->
                > <xsl:template match="/">
                > <xsl:element name="lbgjr">
                > <xsl:apply-templates select="*" />
                > </xsl:element>
                > </xsl:template>
                > <!-- convert each instance node into a node that is the same as the Type
                > attribute -->
                > <!-- from: -->
                > <!-- <instance identity="A1" name="A1" type="A_Type"> -->
                > <!-- <instance identity="B1" name="B1" type="B_Type"> -->
                > <!-- <instance identity="C1" name="C1" type="C_Type"> -->
                > <!-- to: -->
                > <!-- <A_Type identity="A1" name="A1" type="A_Type"> -->
                > <!-- <B_Type identity="B1" name="B1" type="B_Type"> -->
                > <!-- <C_Type identity="C1" name="C1" type="C_Type"> -->
                > <xsl:template match="instance ">
                > <xsl:element name="{@type}">
                > <xsl:for-each select="@*">
                > <xsl:attribut e name="{local-name(.)}">
                > <xsl:value-of select="." />
                > </xsl:attribute>
                > </xsl:for-each>
                > <xsl:apply-templates select="*" />
                > </xsl:element>
                > </xsl:template>
                > <!-- convert each Attribute node into a node that has the same name as the
                > attribute -->
                > <!-- from: -->
                > <!-- <attribute Name="AttA1-1"> -->
                > <!-- <value>X</value> -->
                > <!-- </attribute> -->
                > <!-- to: -->
                > <!-- <AttA1-1>X</AttA1-1> -->
                > <xsl:template match="attribut e">
                > <xsl:element name="{@Name}">
                > <xsl:value-of select="value" />
                > </xsl:element>
                > </xsl:template>
                > </xsl:stylesheet>
                >
                > You can call it with code similar to:
                >
                > Dim xslt As New XslTransform
                > xslt.Load("lgbj r.xslt")
                > xslt.Transform( "lgbjr.xml" , "lgbjr.output.x ml", Nothing)
                > Dim ds As New DataSet
                > ds.ReadXml("lgb jr.output.xml")
                >
                > I've only minimally tested the above template, as your sample data is not
                > as
                > well formed as the description you gave...
                >
                > Hope this helps
                > Jay
                >
                > "lgbjr" <lgbjr@online.n ospam> wrote in message
                > news:umq%23sSTW FHA.2540@tk2msf tngp13.phx.gbl. ..
                > | Hi All,
                > |
                > | I've been given an XML file (12 MB) that I need to extract data from.
                > The
                > | XML file makes the data easy to view, but the data is impossible to
                > analyze
                > | in its current format. I've looked at lots of XMLTextReader stuff and I
                > | can't seem to figure out how to extract the info that I want. Here's a
                > quick
                > | example of the XML format:
                > |
                > | <instance identity="A1" name="A1" type="A_Type">
                > | <attribute Name="AttA1-1">
                > | <value>
                > | X
                > | </value>
                > | </attribute>
                > | <instance identity="B1" name="B1" type="B_Type">
                > | <attribute Name="AttB1-1">
                > | <value>
                > | X
                > | </value>
                > | </attribute>
                > | <instance identity="B2" name="B2" type="B_Type">
                > | <attribute Name="AttB2-1">
                > | <value>
                > | X
                > | </value>
                > | </attribute>
                > | <instance identity="C1" name="C1" type="C_Type">
                > | <attribute Name="AttC1-1">
                > | <value>
                > | X
                > | </value>
                > | </attribute>
                > | </instance>
                > | </instance>
                > | </instance>
                > | </instance>
                > |
                > | Each A_Type contains several B_Types, and each B_Type contains several
                > | C_Types and the file contains lots of A_Types. I've shown one attribute
                > | each, but there are lots of them for each type!
                > |
                > | what I'd like to do is load this data into a few tables with
                > Master-Detail
                > | Releations. So Table A would contain a row for each A_Type plus it's
                > | attributes (with the Attribute Name as the Column Header). For a given
                > row
                > | selected in Table A, Table B would show each of the B_Types and it's
                > | attributes, etc.
                > |
                > | So, is there an easy way to get the data from the file using an
                > | XMLTextReader or some other nifty XML tool, or is this going to be a
                > brute
                > | force method with regular expressions and do loops?
                > |
                > | I'm not sure if I've supplied enough info, so if you need to know
                > something
                > | else before you can give me an answer, please let me know!! If it's
                > going
                > to
                > | be brute force, that's fine, but I'd hate to spend a few days coding,
                > only
                > | later to find out that there was a quick way to do this!
                > |
                > | TIA
                > | Lee
                > |
                > |
                >
                >[/color]


                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: XML to DataSets

                  lgbjr,
                  The error itself is because I used "Name" for the "name" attribute of
                  Attributes.

                  However once that is fixed you will have another problem, your original XML
                  did not include the "type" nodes for Attributes.

                  Your original XML had:

                  | > | <attribute Name="AttB1-1">
                  | > | <value>
                  | > | X
                  | > | </value>
                  | > | </attribute>

                  While your "actual" XML has:

                  | <attribute name="BSSId">
                  | <datatype>
                  | <string>
                  | <value>
                  | BSS_TaiZhouBSS0 1
                  | </value>
                  | </string>
                  | </datatype>
                  | </attribute>

                  Notice the <datatype> & <string> nodes in the above. Also it appears your
                  original includes both a document node of <top> and another node of <model>.

                  Here is an updated XSLT:

                  <?xml version="1.0" encoding="UTF-8" ?>
                  <xsl:styleshe et version="1.0"
                  xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
                  <!-- handle the document/root node of the input -->
                  <xsl:template match="/">
                  <xsl:apply-templates select="*" />
                  </xsl:template>
                  <!-- copy the top node "as is" -->
                  <xsl:template match="top">
                  <xsl:element name="top">
                  <xsl:copy-of select="@*" />
                  <xsl:apply-templates select="*" />
                  </xsl:element>
                  </xsl:template>
                  <!-- copy the model node "as is" -->
                  <xsl:template match="model">
                  <xsl:element name="model">
                  <xsl:copy-of select="@*" />
                  <xsl:apply-templates select="*" />
                  </xsl:element>
                  </xsl:template>
                  <!-- convert each instance node into a node that is the same as the Type
                  attribute -->
                  <xsl:template match="instance ">
                  <xsl:element name="{@type}">
                  <xsl:copy-of select="@*" />
                  <xsl:apply-templates select="*" />
                  </xsl:element>
                  </xsl:template>
                  <!-- convert each Attribute node into a node that has the same name as the
                  attribute -->
                  <xsl:template match="attribut e">
                  <xsl:element name="{@name}">
                  <xsl:attribut e name="xsi:type"
                  namespace="http ://www.w3.org/2001/XMLSchema-instance">
                  <xsl:choose>
                  <xsl:when test="local-name(datatype/*)='enumRef'">
                  <xsl:value-of select="datatyp e/*/@name" />
                  </xsl:when>
                  <xsl:otherwis e>
                  <xsl:value-of select="local-name(datatype/*)" />
                  </xsl:otherwise>
                  </xsl:choose>
                  </xsl:attribute>
                  <xsl:value-of select="datatyp e/*/value" />
                  </xsl:element>
                  </xsl:template>
                  </xsl:stylesheet>

                  NOTE: I simply copy the <top> & <model> nodes as is. I "preserve" the type
                  of the individual Attributes, as an attribute of that Attribute.

                  This:
                  | <attribute name="BSSId">
                  | <datatype>
                  | <string>
                  | <value>
                  | BSS_TaiZhouBSS0 1
                  | </value>
                  | </string>
                  | </datatype>
                  | </attribute>

                  Becomes this:

                  <BSSId type="string">B SS_TaiZhouBSS01 </BSSId>

                  Note: The type attribute in the above, should probably be xsi:type!

                  Instead of:
                  <xsl:element name="{@name}">
                  <xsl:attribut e name="xsi:type"
                  namespace="http ://www.w3.org/2001/XMLSchema-instance">

                  You can use:
                  <xsl:element name="{@name}">
                  <xsl:attribut e name="type">

                  To remove all of the xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
                  in the output, ideally xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
                  should be included once on <top>.

                  Hope this helps
                  Jay


                  "lgbjr" <lgbjr@online.n ospam> wrote in message
                  news:uoZfhwcWFH A.1404@TK2MSFTN GP09.phx.gbl...
                  | Hi All,
                  |
                  | OK, I'm starting to understand a bit of what I need to do (and why). When
                  I
                  | add the XML file to the project, open it, then right click in the view and
                  | select View Data Grid, I get an error that says "The Table (instance)
                  cannot
                  | be the child table to itself in nested relations. this makes sense based
                  on
                  | the way the XML file is setup.
                  |
                  | So, I need to do as Jay recommended, which is to transform each instance
                  to
                  | its type and each attribute to its name. Then, each instance will be a
                  table
                  | (multiples of the same instance will be rows in the table) and each
                  | attribute in an instance will be a column in the table.
                  |
                  | So, I'm working with Jay's XSLT file as a start and am using the bit of
                  code
                  | that he supplied to load the XSLT file, then do the transform.
                  |
                  | Here's the first problem. when I run the transform, I get an exception:
                  |
                  | The empty string '' is not a valid name.
                  |
                  | And the output XML file is obviously incomplete.
                  |
                  | This is the top of the XML file that I want to transform:
                  |
                  | <?xml version="1.0" ?>
                  | <top>
                  | <model mim="CO_BSSMIM" name="MIB_BSS_B SS_TaiZhouBSS01 _13314645990348 0000"
                  | prefixDN="RMA=R adio_Access_Net work" release="UNDEFI NED" rootLDN=""
                  | version="2.2">
                  | <instance identity="insta nce_1151" name="BSS_TaiZh ouBSS01" type="BSS">
                  | <attribute name="BSSId">
                  | <datatype>
                  | <string>
                  | <value>
                  | BSS_TaiZhouBSS0 1
                  | </value>
                  | </string>
                  | </datatype>
                  | </attribute> <instance identity="insta nce_0" name="RBS_H101"
                  | type="RBS">
                  | <attribute name="RBSId">
                  | <datatype>
                  | <string>
                  | <value>
                  | RBS_H101
                  | </value>
                  | </string>
                  | </datatype>
                  | </attribute> <attribute name="RBSModel" >
                  | <datatype>
                  | <enumRef name="RBSModelT ype">
                  | <value>
                  | 1
                  | </value>
                  | </enumRef>
                  | </datatype>
                  | </attribute> <attribute name="SectorCon figuration">
                  | <datatype>
                  | <enumRef name="SectorCon figurationType" >
                  | <value>
                  | 0
                  | </value>
                  | </enumRef>
                  | </datatype>
                  | </attribute>
                  |
                  | this is the output XML file:
                  |
                  | <?xml version="1.0" encoding="utf-8"?><lgbjr>
                  |
                  | <BSS
                  |
                  | There are no trailing spaces in the input XML file. The transform is
                  failing
                  | at the first transformation. Can someone tell me why?
                  |
                  | thanks!!
                  |
                  | Lee
                  |
                  | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                  | news:evJxLzZWFH A.1404@TK2MSFTN GP09.phx.gbl...
                  | > lgbjr,
                  | > I would consider using an XSLT transform to transform your input data
                  into
                  | > something a little more DataSet friendly. Here is the start of such a
                  | > transform:
                  | >
                  | > <?xml version="1.0" encoding="UTF-8" ?>
                  | > <xsl:styleshe et version="1.0"
                  | > xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
                  | > <!-- handle the document/root node of the input -->
                  | > <xsl:template match="/">
                  | > <xsl:element name="lbgjr">
                  | > <xsl:apply-templates select="*" />
                  | > </xsl:element>
                  | > </xsl:template>
                  | > <!-- convert each instance node into a node that is the same as the Type
                  | > attribute -->
                  | > <!-- from: -->
                  | > <!-- <instance identity="A1" name="A1" type="A_Type"> -->
                  | > <!-- <instance identity="B1" name="B1" type="B_Type"> -->
                  | > <!-- <instance identity="C1" name="C1" type="C_Type"> -->
                  | > <!-- to: -->
                  | > <!-- <A_Type identity="A1" name="A1" type="A_Type"> -->
                  | > <!-- <B_Type identity="B1" name="B1" type="B_Type"> -->
                  | > <!-- <C_Type identity="C1" name="C1" type="C_Type"> -->
                  | > <xsl:template match="instance ">
                  | > <xsl:element name="{@type}">
                  | > <xsl:for-each select="@*">
                  | > <xsl:attribut e name="{local-name(.)}">
                  | > <xsl:value-of select="." />
                  | > </xsl:attribute>
                  | > </xsl:for-each>
                  | > <xsl:apply-templates select="*" />
                  | > </xsl:element>
                  | > </xsl:template>
                  | > <!-- convert each Attribute node into a node that has the same name as
                  the
                  | > attribute -->
                  | > <!-- from: -->
                  | > <!-- <attribute Name="AttA1-1"> -->
                  | > <!-- <value>X</value> -->
                  | > <!-- </attribute> -->
                  | > <!-- to: -->
                  | > <!-- <AttA1-1>X</AttA1-1> -->
                  | > <xsl:template match="attribut e">
                  | > <xsl:element name="{@Name}">
                  | > <xsl:value-of select="value" />
                  | > </xsl:element>
                  | > </xsl:template>
                  | > </xsl:stylesheet>
                  | >
                  | > You can call it with code similar to:
                  | >
                  | > Dim xslt As New XslTransform
                  | > xslt.Load("lgbj r.xslt")
                  | > xslt.Transform( "lgbjr.xml" , "lgbjr.output.x ml", Nothing)
                  | > Dim ds As New DataSet
                  | > ds.ReadXml("lgb jr.output.xml")
                  | >
                  | > I've only minimally tested the above template, as your sample data is
                  not
                  | > as
                  | > well formed as the description you gave...
                  | >
                  | > Hope this helps
                  | > Jay
                  | >
                  | > "lgbjr" <lgbjr@online.n ospam> wrote in message
                  | > news:umq%23sSTW FHA.2540@tk2msf tngp13.phx.gbl. ..
                  | > | Hi All,
                  | > |
                  | > | I've been given an XML file (12 MB) that I need to extract data from.
                  | > The
                  | > | XML file makes the data easy to view, but the data is impossible to
                  | > analyze
                  | > | in its current format. I've looked at lots of XMLTextReader stuff and
                  I
                  | > | can't seem to figure out how to extract the info that I want. Here's a
                  | > quick
                  | > | example of the XML format:
                  | > |
                  | > | <instance identity="A1" name="A1" type="A_Type">
                  | > | <attribute Name="AttA1-1">
                  | > | <value>
                  | > | X
                  | > | </value>
                  | > | </attribute>
                  | > | <instance identity="B1" name="B1" type="B_Type">
                  | > | <attribute Name="AttB1-1">
                  | > | <value>
                  | > | X
                  | > | </value>
                  | > | </attribute>
                  | > | <instance identity="B2" name="B2" type="B_Type">
                  | > | <attribute Name="AttB2-1">
                  | > | <value>
                  | > | X
                  | > | </value>
                  | > | </attribute>
                  | > | <instance identity="C1" name="C1" type="C_Type">
                  | > | <attribute Name="AttC1-1">
                  | > | <value>
                  | > | X
                  | > | </value>
                  | > | </attribute>
                  | > | </instance>
                  | > | </instance>
                  | > | </instance>
                  | > | </instance>
                  | > |
                  | > | Each A_Type contains several B_Types, and each B_Type contains several
                  | > | C_Types and the file contains lots of A_Types. I've shown one
                  attribute
                  | > | each, but there are lots of them for each type!
                  | > |
                  | > | what I'd like to do is load this data into a few tables with
                  | > Master-Detail
                  | > | Releations. So Table A would contain a row for each A_Type plus it's
                  | > | attributes (with the Attribute Name as the Column Header). For a given
                  | > row
                  | > | selected in Table A, Table B would show each of the B_Types and it's
                  | > | attributes, etc.
                  | > |
                  | > | So, is there an easy way to get the data from the file using an
                  | > | XMLTextReader or some other nifty XML tool, or is this going to be a
                  | > brute
                  | > | force method with regular expressions and do loops?
                  | > |
                  | > | I'm not sure if I've supplied enough info, so if you need to know
                  | > something
                  | > | else before you can give me an answer, please let me know!! If it's
                  | > going
                  | > to
                  | > | be brute force, that's fine, but I'd hate to spend a few days coding,
                  | > only
                  | > | later to find out that there was a quick way to do this!
                  | > |
                  | > | TIA
                  | > | Lee
                  | > |
                  | > |
                  | >
                  | >
                  |
                  |


                  Comment

                  • lgbjr

                    #10
                    Re: XML to DataSets

                    Hi Jay,

                    Thank you very much for the help. I've been using VB.NET for 2 years now, so
                    I've been surrounded by XML, but never actually used it! Now I've got a
                    requirement to use it and I'm having to jump in with both feet. I'm sure
                    that playing with all of the nice little examples in the docs and on the web
                    would have eventually gotten me to this point, but honestly, now, I just
                    don't have the time. Once I get this finished, I'll go back and do the step
                    by step stuff (which I'm sure will lead to a whole set of new questions!)
                    Thanks for getting me in the fast lane on this stuff. I appreciate that your
                    posts include not only code, but comments as to why certain things are
                    there. This makes a big difference to me as it's easier to understand why
                    things are done a certain way.

                    Sorry for not including the same XML code in the original post. Again, as
                    this is new to me, I didn't really know what was important and what wasn't.
                    I opted for being as brief as possible the first time. Now, I know better!

                    Thanks Again,
                    Lee

                    "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                    news:O9S0q9hWFH A.2684@TK2MSFTN GP09.phx.gbl...[color=blue]
                    > lgbjr,
                    > The error itself is because I used "Name" for the "name" attribute of
                    > Attributes.
                    >
                    > However once that is fixed you will have another problem, your original
                    > XML
                    > did not include the "type" nodes for Attributes.
                    >
                    > Your original XML had:
                    >
                    > | > | <attribute Name="AttB1-1">
                    > | > | <value>
                    > | > | X
                    > | > | </value>
                    > | > | </attribute>
                    >
                    > While your "actual" XML has:
                    >
                    > | <attribute name="BSSId">
                    > | <datatype>
                    > | <string>
                    > | <value>
                    > | BSS_TaiZhouBSS0 1
                    > | </value>
                    > | </string>
                    > | </datatype>
                    > | </attribute>
                    >
                    > Notice the <datatype> & <string> nodes in the above. Also it appears your
                    > original includes both a document node of <top> and another node of
                    > <model>.
                    >
                    > Here is an updated XSLT:
                    >
                    > <?xml version="1.0" encoding="UTF-8" ?>
                    > <xsl:styleshe et version="1.0"
                    > xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
                    > <!-- handle the document/root node of the input -->
                    > <xsl:template match="/">
                    > <xsl:apply-templates select="*" />
                    > </xsl:template>
                    > <!-- copy the top node "as is" -->
                    > <xsl:template match="top">
                    > <xsl:element name="top">
                    > <xsl:copy-of select="@*" />
                    > <xsl:apply-templates select="*" />
                    > </xsl:element>
                    > </xsl:template>
                    > <!-- copy the model node "as is" -->
                    > <xsl:template match="model">
                    > <xsl:element name="model">
                    > <xsl:copy-of select="@*" />
                    > <xsl:apply-templates select="*" />
                    > </xsl:element>
                    > </xsl:template>
                    > <!-- convert each instance node into a node that is the same as the Type
                    > attribute -->
                    > <xsl:template match="instance ">
                    > <xsl:element name="{@type}">
                    > <xsl:copy-of select="@*" />
                    > <xsl:apply-templates select="*" />
                    > </xsl:element>
                    > </xsl:template>
                    > <!-- convert each Attribute node into a node that has the same name as the
                    > attribute -->
                    > <xsl:template match="attribut e">
                    > <xsl:element name="{@name}">
                    > <xsl:attribut e name="xsi:type"
                    > namespace="http ://www.w3.org/2001/XMLSchema-instance">
                    > <xsl:choose>
                    > <xsl:when test="local-name(datatype/*)='enumRef'">
                    > <xsl:value-of select="datatyp e/*/@name" />
                    > </xsl:when>
                    > <xsl:otherwis e>
                    > <xsl:value-of select="local-name(datatype/*)" />
                    > </xsl:otherwise>
                    > </xsl:choose>
                    > </xsl:attribute>
                    > <xsl:value-of select="datatyp e/*/value" />
                    > </xsl:element>
                    > </xsl:template>
                    > </xsl:stylesheet>
                    >
                    > NOTE: I simply copy the <top> & <model> nodes as is. I "preserve" the type
                    > of the individual Attributes, as an attribute of that Attribute.
                    >
                    > This:
                    > | <attribute name="BSSId">
                    > | <datatype>
                    > | <string>
                    > | <value>
                    > | BSS_TaiZhouBSS0 1
                    > | </value>
                    > | </string>
                    > | </datatype>
                    > | </attribute>
                    >
                    > Becomes this:
                    >
                    > <BSSId type="string">B SS_TaiZhouBSS01 </BSSId>
                    >
                    > Note: The type attribute in the above, should probably be xsi:type!
                    >
                    > Instead of:
                    > <xsl:element name="{@name}">
                    > <xsl:attribut e name="xsi:type"
                    > namespace="http ://www.w3.org/2001/XMLSchema-instance">
                    >
                    > You can use:
                    > <xsl:element name="{@name}">
                    > <xsl:attribut e name="type">
                    >
                    > To remove all of the xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
                    > in the output, ideally
                    > xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
                    > should be included once on <top>.
                    >
                    > Hope this helps
                    > Jay
                    >
                    >
                    > "lgbjr" <lgbjr@online.n ospam> wrote in message
                    > news:uoZfhwcWFH A.1404@TK2MSFTN GP09.phx.gbl...
                    > | Hi All,
                    > |
                    > | OK, I'm starting to understand a bit of what I need to do (and why).
                    > When
                    > I
                    > | add the XML file to the project, open it, then right click in the view
                    > and
                    > | select View Data Grid, I get an error that says "The Table (instance)
                    > cannot
                    > | be the child table to itself in nested relations. this makes sense based
                    > on
                    > | the way the XML file is setup.
                    > |
                    > | So, I need to do as Jay recommended, which is to transform each instance
                    > to
                    > | its type and each attribute to its name. Then, each instance will be a
                    > table
                    > | (multiples of the same instance will be rows in the table) and each
                    > | attribute in an instance will be a column in the table.
                    > |
                    > | So, I'm working with Jay's XSLT file as a start and am using the bit of
                    > code
                    > | that he supplied to load the XSLT file, then do the transform.
                    > |
                    > | Here's the first problem. when I run the transform, I get an exception:
                    > |
                    > | The empty string '' is not a valid name.
                    > |
                    > | And the output XML file is obviously incomplete.
                    > |
                    > | This is the top of the XML file that I want to transform:
                    > |
                    > | <?xml version="1.0" ?>
                    > | <top>
                    > | <model mim="CO_BSSMIM"
                    > name="MIB_BSS_B SS_TaiZhouBSS01 _13314645990348 0000"
                    > | prefixDN="RMA=R adio_Access_Net work" release="UNDEFI NED" rootLDN=""
                    > | version="2.2">
                    > | <instance identity="insta nce_1151" name="BSS_TaiZh ouBSS01" type="BSS">
                    > | <attribute name="BSSId">
                    > | <datatype>
                    > | <string>
                    > | <value>
                    > | BSS_TaiZhouBSS0 1
                    > | </value>
                    > | </string>
                    > | </datatype>
                    > | </attribute> <instance identity="insta nce_0" name="RBS_H101"
                    > | type="RBS">
                    > | <attribute name="RBSId">
                    > | <datatype>
                    > | <string>
                    > | <value>
                    > | RBS_H101
                    > | </value>
                    > | </string>
                    > | </datatype>
                    > | </attribute> <attribute name="RBSModel" >
                    > | <datatype>
                    > | <enumRef name="RBSModelT ype">
                    > | <value>
                    > | 1
                    > | </value>
                    > | </enumRef>
                    > | </datatype>
                    > | </attribute> <attribute name="SectorCon figuration">
                    > | <datatype>
                    > | <enumRef name="SectorCon figurationType" >
                    > | <value>
                    > | 0
                    > | </value>
                    > | </enumRef>
                    > | </datatype>
                    > | </attribute>
                    > |
                    > | this is the output XML file:
                    > |
                    > | <?xml version="1.0" encoding="utf-8"?><lgbjr>
                    > |
                    > | <BSS
                    > |
                    > | There are no trailing spaces in the input XML file. The transform is
                    > failing
                    > | at the first transformation. Can someone tell me why?
                    > |
                    > | thanks!!
                    > |
                    > | Lee
                    > |
                    > | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in
                    > message
                    > | news:evJxLzZWFH A.1404@TK2MSFTN GP09.phx.gbl...
                    > | > lgbjr,
                    > | > I would consider using an XSLT transform to transform your input data
                    > into
                    > | > something a little more DataSet friendly. Here is the start of such a
                    > | > transform:
                    > | >
                    > | > <?xml version="1.0" encoding="UTF-8" ?>
                    > | > <xsl:styleshe et version="1.0"
                    > | > xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
                    > | > <!-- handle the document/root node of the input -->
                    > | > <xsl:template match="/">
                    > | > <xsl:element name="lbgjr">
                    > | > <xsl:apply-templates select="*" />
                    > | > </xsl:element>
                    > | > </xsl:template>
                    > | > <!-- convert each instance node into a node that is the same as the
                    > Type
                    > | > attribute -->
                    > | > <!-- from: -->
                    > | > <!-- <instance identity="A1" name="A1" type="A_Type"> -->
                    > | > <!-- <instance identity="B1" name="B1" type="B_Type"> -->
                    > | > <!-- <instance identity="C1" name="C1" type="C_Type"> -->
                    > | > <!-- to: -->
                    > | > <!-- <A_Type identity="A1" name="A1" type="A_Type"> -->
                    > | > <!-- <B_Type identity="B1" name="B1" type="B_Type"> -->
                    > | > <!-- <C_Type identity="C1" name="C1" type="C_Type"> -->
                    > | > <xsl:template match="instance ">
                    > | > <xsl:element name="{@type}">
                    > | > <xsl:for-each select="@*">
                    > | > <xsl:attribut e name="{local-name(.)}">
                    > | > <xsl:value-of select="." />
                    > | > </xsl:attribute>
                    > | > </xsl:for-each>
                    > | > <xsl:apply-templates select="*" />
                    > | > </xsl:element>
                    > | > </xsl:template>
                    > | > <!-- convert each Attribute node into a node that has the same name as
                    > the
                    > | > attribute -->
                    > | > <!-- from: -->
                    > | > <!-- <attribute Name="AttA1-1"> -->
                    > | > <!-- <value>X</value> -->
                    > | > <!-- </attribute> -->
                    > | > <!-- to: -->
                    > | > <!-- <AttA1-1>X</AttA1-1> -->
                    > | > <xsl:template match="attribut e">
                    > | > <xsl:element name="{@Name}">
                    > | > <xsl:value-of select="value" />
                    > | > </xsl:element>
                    > | > </xsl:template>
                    > | > </xsl:stylesheet>
                    > | >
                    > | > You can call it with code similar to:
                    > | >
                    > | > Dim xslt As New XslTransform
                    > | > xslt.Load("lgbj r.xslt")
                    > | > xslt.Transform( "lgbjr.xml" , "lgbjr.output.x ml", Nothing)
                    > | > Dim ds As New DataSet
                    > | > ds.ReadXml("lgb jr.output.xml")
                    > | >
                    > | > I've only minimally tested the above template, as your sample data is
                    > not
                    > | > as
                    > | > well formed as the description you gave...
                    > | >
                    > | > Hope this helps
                    > | > Jay
                    > | >
                    > | > "lgbjr" <lgbjr@online.n ospam> wrote in message
                    > | > news:umq%23sSTW FHA.2540@tk2msf tngp13.phx.gbl. ..
                    > | > | Hi All,
                    > | > |
                    > | > | I've been given an XML file (12 MB) that I need to extract data
                    > from.
                    > | > The
                    > | > | XML file makes the data easy to view, but the data is impossible to
                    > | > analyze
                    > | > | in its current format. I've looked at lots of XMLTextReader stuff
                    > and
                    > I
                    > | > | can't seem to figure out how to extract the info that I want. Here's
                    > a
                    > | > quick
                    > | > | example of the XML format:
                    > | > |
                    > | > | <instance identity="A1" name="A1" type="A_Type">
                    > | > | <attribute Name="AttA1-1">
                    > | > | <value>
                    > | > | X
                    > | > | </value>
                    > | > | </attribute>
                    > | > | <instance identity="B1" name="B1" type="B_Type">
                    > | > | <attribute Name="AttB1-1">
                    > | > | <value>
                    > | > | X
                    > | > | </value>
                    > | > | </attribute>
                    > | > | <instance identity="B2" name="B2" type="B_Type">
                    > | > | <attribute Name="AttB2-1">
                    > | > | <value>
                    > | > | X
                    > | > | </value>
                    > | > | </attribute>
                    > | > | <instance identity="C1" name="C1" type="C_Type">
                    > | > | <attribute Name="AttC1-1">
                    > | > | <value>
                    > | > | X
                    > | > | </value>
                    > | > | </attribute>
                    > | > | </instance>
                    > | > | </instance>
                    > | > | </instance>
                    > | > | </instance>
                    > | > |
                    > | > | Each A_Type contains several B_Types, and each B_Type contains
                    > several
                    > | > | C_Types and the file contains lots of A_Types. I've shown one
                    > attribute
                    > | > | each, but there are lots of them for each type!
                    > | > |
                    > | > | what I'd like to do is load this data into a few tables with
                    > | > Master-Detail
                    > | > | Releations. So Table A would contain a row for each A_Type plus it's
                    > | > | attributes (with the Attribute Name as the Column Header). For a
                    > given
                    > | > row
                    > | > | selected in Table A, Table B would show each of the B_Types and it's
                    > | > | attributes, etc.
                    > | > |
                    > | > | So, is there an easy way to get the data from the file using an
                    > | > | XMLTextReader or some other nifty XML tool, or is this going to be a
                    > | > brute
                    > | > | force method with regular expressions and do loops?
                    > | > |
                    > | > | I'm not sure if I've supplied enough info, so if you need to know
                    > | > something
                    > | > | else before you can give me an answer, please let me know!! If it's
                    > | > going
                    > | > to
                    > | > | be brute force, that's fine, but I'd hate to spend a few days
                    > coding,
                    > | > only
                    > | > | later to find out that there was a quick way to do this!
                    > | > |
                    > | > | TIA
                    > | > | Lee
                    > | > |
                    > | > |
                    > | >
                    > | >
                    > |
                    > |
                    >
                    >[/color]


                    Comment

                    • lgbjr

                      #11
                      Re: XML to DataSets

                      Hi Jay,

                      OK, so far, so good. Using your XSLT as a start, I added some more
                      transforms (for things that where not included in the previous posts. So,
                      the XSLT works, I can read the resulting XML file (dataset.ReadXM L), and I
                      can create an XSD file (dataset.WriteX mlSchema). As a test, I imported the
                      resulting Schema into Access, then imported the XML file (append data to
                      exisiting tables) and everything looks good.

                      After seeing that the results were good, I went back to my small transform
                      app and added a shell to XSD.exe to generate a typed dataset from the XSD
                      file.

                      Now, my thinking is that using a typed data set is the easiest way to go for
                      use in a new app. I just add the typed dataset, and the rest is pretty easy.
                      But, of course, as soon as I realize something will be easy, it doesn't
                      work!! XSD.exe creates a vb file that I added to a new app. then I dropped a
                      dataset on my form and selected the typed dataset, but I get an error saying
                      "Exception has been thrown by the target of an invocation". Not very
                      descriptive!!

                      Now, temporarily, forgetting the typed dataset, if I do a dataset.readxml , I
                      have to dynamically create each table and column and relationship that I
                      want to see in a grid. If I add the XSD file to my project, how can I use
                      the tables/columns/relationships defined in the XSD file? Or can I only do
                      this with a typed dataset created from the XSD file?

                      TIA,
                      Lee

                      "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                      news:O9S0q9hWFH A.2684@TK2MSFTN GP09.phx.gbl...[color=blue]
                      > lgbjr,
                      > The error itself is because I used "Name" for the "name" attribute of
                      > Attributes.
                      >
                      > However once that is fixed you will have another problem, your original
                      > XML
                      > did not include the "type" nodes for Attributes.
                      >
                      > Your original XML had:
                      >
                      > | > | <attribute Name="AttB1-1">
                      > | > | <value>
                      > | > | X
                      > | > | </value>
                      > | > | </attribute>
                      >
                      > While your "actual" XML has:
                      >
                      > | <attribute name="BSSId">
                      > | <datatype>
                      > | <string>
                      > | <value>
                      > | BSS_TaiZhouBSS0 1
                      > | </value>
                      > | </string>
                      > | </datatype>
                      > | </attribute>
                      >
                      > Notice the <datatype> & <string> nodes in the above. Also it appears your
                      > original includes both a document node of <top> and another node of
                      > <model>.
                      >
                      > Here is an updated XSLT:
                      >
                      > <?xml version="1.0" encoding="UTF-8" ?>
                      > <xsl:styleshe et version="1.0"
                      > xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
                      > <!-- handle the document/root node of the input -->
                      > <xsl:template match="/">
                      > <xsl:apply-templates select="*" />
                      > </xsl:template>
                      > <!-- copy the top node "as is" -->
                      > <xsl:template match="top">
                      > <xsl:element name="top">
                      > <xsl:copy-of select="@*" />
                      > <xsl:apply-templates select="*" />
                      > </xsl:element>
                      > </xsl:template>
                      > <!-- copy the model node "as is" -->
                      > <xsl:template match="model">
                      > <xsl:element name="model">
                      > <xsl:copy-of select="@*" />
                      > <xsl:apply-templates select="*" />
                      > </xsl:element>
                      > </xsl:template>
                      > <!-- convert each instance node into a node that is the same as the Type
                      > attribute -->
                      > <xsl:template match="instance ">
                      > <xsl:element name="{@type}">
                      > <xsl:copy-of select="@*" />
                      > <xsl:apply-templates select="*" />
                      > </xsl:element>
                      > </xsl:template>
                      > <!-- convert each Attribute node into a node that has the same name as the
                      > attribute -->
                      > <xsl:template match="attribut e">
                      > <xsl:element name="{@name}">
                      > <xsl:attribut e name="xsi:type"
                      > namespace="http ://www.w3.org/2001/XMLSchema-instance">
                      > <xsl:choose>
                      > <xsl:when test="local-name(datatype/*)='enumRef'">
                      > <xsl:value-of select="datatyp e/*/@name" />
                      > </xsl:when>
                      > <xsl:otherwis e>
                      > <xsl:value-of select="local-name(datatype/*)" />
                      > </xsl:otherwise>
                      > </xsl:choose>
                      > </xsl:attribute>
                      > <xsl:value-of select="datatyp e/*/value" />
                      > </xsl:element>
                      > </xsl:template>
                      > </xsl:stylesheet>
                      >
                      > NOTE: I simply copy the <top> & <model> nodes as is. I "preserve" the type
                      > of the individual Attributes, as an attribute of that Attribute.
                      >
                      > This:
                      > | <attribute name="BSSId">
                      > | <datatype>
                      > | <string>
                      > | <value>
                      > | BSS_TaiZhouBSS0 1
                      > | </value>
                      > | </string>
                      > | </datatype>
                      > | </attribute>
                      >
                      > Becomes this:
                      >
                      > <BSSId type="string">B SS_TaiZhouBSS01 </BSSId>
                      >
                      > Note: The type attribute in the above, should probably be xsi:type!
                      >
                      > Instead of:
                      > <xsl:element name="{@name}">
                      > <xsl:attribut e name="xsi:type"
                      > namespace="http ://www.w3.org/2001/XMLSchema-instance">
                      >
                      > You can use:
                      > <xsl:element name="{@name}">
                      > <xsl:attribut e name="type">
                      >
                      > To remove all of the xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
                      > in the output, ideally
                      > xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
                      > should be included once on <top>.
                      >
                      > Hope this helps
                      > Jay
                      >
                      >
                      > "lgbjr" <lgbjr@online.n ospam> wrote in message
                      > news:uoZfhwcWFH A.1404@TK2MSFTN GP09.phx.gbl...
                      > | Hi All,
                      > |
                      > | OK, I'm starting to understand a bit of what I need to do (and why).
                      > When
                      > I
                      > | add the XML file to the project, open it, then right click in the view
                      > and
                      > | select View Data Grid, I get an error that says "The Table (instance)
                      > cannot
                      > | be the child table to itself in nested relations. this makes sense based
                      > on
                      > | the way the XML file is setup.
                      > |
                      > | So, I need to do as Jay recommended, which is to transform each instance
                      > to
                      > | its type and each attribute to its name. Then, each instance will be a
                      > table
                      > | (multiples of the same instance will be rows in the table) and each
                      > | attribute in an instance will be a column in the table.
                      > |
                      > | So, I'm working with Jay's XSLT file as a start and am using the bit of
                      > code
                      > | that he supplied to load the XSLT file, then do the transform.
                      > |
                      > | Here's the first problem. when I run the transform, I get an exception:
                      > |
                      > | The empty string '' is not a valid name.
                      > |
                      > | And the output XML file is obviously incomplete.
                      > |
                      > | This is the top of the XML file that I want to transform:
                      > |
                      > | <?xml version="1.0" ?>
                      > | <top>
                      > | <model mim="CO_BSSMIM"
                      > name="MIB_BSS_B SS_TaiZhouBSS01 _13314645990348 0000"
                      > | prefixDN="RMA=R adio_Access_Net work" release="UNDEFI NED" rootLDN=""
                      > | version="2.2">
                      > | <instance identity="insta nce_1151" name="BSS_TaiZh ouBSS01" type="BSS">
                      > | <attribute name="BSSId">
                      > | <datatype>
                      > | <string>
                      > | <value>
                      > | BSS_TaiZhouBSS0 1
                      > | </value>
                      > | </string>
                      > | </datatype>
                      > | </attribute> <instance identity="insta nce_0" name="RBS_H101"
                      > | type="RBS">
                      > | <attribute name="RBSId">
                      > | <datatype>
                      > | <string>
                      > | <value>
                      > | RBS_H101
                      > | </value>
                      > | </string>
                      > | </datatype>
                      > | </attribute> <attribute name="RBSModel" >
                      > | <datatype>
                      > | <enumRef name="RBSModelT ype">
                      > | <value>
                      > | 1
                      > | </value>
                      > | </enumRef>
                      > | </datatype>
                      > | </attribute> <attribute name="SectorCon figuration">
                      > | <datatype>
                      > | <enumRef name="SectorCon figurationType" >
                      > | <value>
                      > | 0
                      > | </value>
                      > | </enumRef>
                      > | </datatype>
                      > | </attribute>
                      > |
                      > | this is the output XML file:
                      > |
                      > | <?xml version="1.0" encoding="utf-8"?><lgbjr>
                      > |
                      > | <BSS
                      > |
                      > | There are no trailing spaces in the input XML file. The transform is
                      > failing
                      > | at the first transformation. Can someone tell me why?
                      > |
                      > | thanks!!
                      > |
                      > | Lee
                      > |
                      > | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in
                      > message
                      > | news:evJxLzZWFH A.1404@TK2MSFTN GP09.phx.gbl...
                      > | > lgbjr,
                      > | > I would consider using an XSLT transform to transform your input data
                      > into
                      > | > something a little more DataSet friendly. Here is the start of such a
                      > | > transform:
                      > | >
                      > | > <?xml version="1.0" encoding="UTF-8" ?>
                      > | > <xsl:styleshe et version="1.0"
                      > | > xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
                      > | > <!-- handle the document/root node of the input -->
                      > | > <xsl:template match="/">
                      > | > <xsl:element name="lbgjr">
                      > | > <xsl:apply-templates select="*" />
                      > | > </xsl:element>
                      > | > </xsl:template>
                      > | > <!-- convert each instance node into a node that is the same as the
                      > Type
                      > | > attribute -->
                      > | > <!-- from: -->
                      > | > <!-- <instance identity="A1" name="A1" type="A_Type"> -->
                      > | > <!-- <instance identity="B1" name="B1" type="B_Type"> -->
                      > | > <!-- <instance identity="C1" name="C1" type="C_Type"> -->
                      > | > <!-- to: -->
                      > | > <!-- <A_Type identity="A1" name="A1" type="A_Type"> -->
                      > | > <!-- <B_Type identity="B1" name="B1" type="B_Type"> -->
                      > | > <!-- <C_Type identity="C1" name="C1" type="C_Type"> -->
                      > | > <xsl:template match="instance ">
                      > | > <xsl:element name="{@type}">
                      > | > <xsl:for-each select="@*">
                      > | > <xsl:attribut e name="{local-name(.)}">
                      > | > <xsl:value-of select="." />
                      > | > </xsl:attribute>
                      > | > </xsl:for-each>
                      > | > <xsl:apply-templates select="*" />
                      > | > </xsl:element>
                      > | > </xsl:template>
                      > | > <!-- convert each Attribute node into a node that has the same name as
                      > the
                      > | > attribute -->
                      > | > <!-- from: -->
                      > | > <!-- <attribute Name="AttA1-1"> -->
                      > | > <!-- <value>X</value> -->
                      > | > <!-- </attribute> -->
                      > | > <!-- to: -->
                      > | > <!-- <AttA1-1>X</AttA1-1> -->
                      > | > <xsl:template match="attribut e">
                      > | > <xsl:element name="{@Name}">
                      > | > <xsl:value-of select="value" />
                      > | > </xsl:element>
                      > | > </xsl:template>
                      > | > </xsl:stylesheet>
                      > | >
                      > | > You can call it with code similar to:
                      > | >
                      > | > Dim xslt As New XslTransform
                      > | > xslt.Load("lgbj r.xslt")
                      > | > xslt.Transform( "lgbjr.xml" , "lgbjr.output.x ml", Nothing)
                      > | > Dim ds As New DataSet
                      > | > ds.ReadXml("lgb jr.output.xml")
                      > | >
                      > | > I've only minimally tested the above template, as your sample data is
                      > not
                      > | > as
                      > | > well formed as the description you gave...
                      > | >
                      > | > Hope this helps
                      > | > Jay
                      > | >
                      > | > "lgbjr" <lgbjr@online.n ospam> wrote in message
                      > | > news:umq%23sSTW FHA.2540@tk2msf tngp13.phx.gbl. ..
                      > | > | Hi All,
                      > | > |
                      > | > | I've been given an XML file (12 MB) that I need to extract data
                      > from.
                      > | > The
                      > | > | XML file makes the data easy to view, but the data is impossible to
                      > | > analyze
                      > | > | in its current format. I've looked at lots of XMLTextReader stuff
                      > and
                      > I
                      > | > | can't seem to figure out how to extract the info that I want. Here's
                      > a
                      > | > quick
                      > | > | example of the XML format:
                      > | > |
                      > | > | <instance identity="A1" name="A1" type="A_Type">
                      > | > | <attribute Name="AttA1-1">
                      > | > | <value>
                      > | > | X
                      > | > | </value>
                      > | > | </attribute>
                      > | > | <instance identity="B1" name="B1" type="B_Type">
                      > | > | <attribute Name="AttB1-1">
                      > | > | <value>
                      > | > | X
                      > | > | </value>
                      > | > | </attribute>
                      > | > | <instance identity="B2" name="B2" type="B_Type">
                      > | > | <attribute Name="AttB2-1">
                      > | > | <value>
                      > | > | X
                      > | > | </value>
                      > | > | </attribute>
                      > | > | <instance identity="C1" name="C1" type="C_Type">
                      > | > | <attribute Name="AttC1-1">
                      > | > | <value>
                      > | > | X
                      > | > | </value>
                      > | > | </attribute>
                      > | > | </instance>
                      > | > | </instance>
                      > | > | </instance>
                      > | > | </instance>
                      > | > |
                      > | > | Each A_Type contains several B_Types, and each B_Type contains
                      > several
                      > | > | C_Types and the file contains lots of A_Types. I've shown one
                      > attribute
                      > | > | each, but there are lots of them for each type!
                      > | > |
                      > | > | what I'd like to do is load this data into a few tables with
                      > | > Master-Detail
                      > | > | Releations. So Table A would contain a row for each A_Type plus it's
                      > | > | attributes (with the Attribute Name as the Column Header). For a
                      > given
                      > | > row
                      > | > | selected in Table A, Table B would show each of the B_Types and it's
                      > | > | attributes, etc.
                      > | > |
                      > | > | So, is there an easy way to get the data from the file using an
                      > | > | XMLTextReader or some other nifty XML tool, or is this going to be a
                      > | > brute
                      > | > | force method with regular expressions and do loops?
                      > | > |
                      > | > | I'm not sure if I've supplied enough info, so if you need to know
                      > | > something
                      > | > | else before you can give me an answer, please let me know!! If it's
                      > | > going
                      > | > to
                      > | > | be brute force, that's fine, but I'd hate to spend a few days
                      > coding,
                      > | > only
                      > | > | later to find out that there was a quick way to do this!
                      > | > |
                      > | > | TIA
                      > | > | Lee
                      > | > |
                      > | > |
                      > | >
                      > | >
                      > |
                      > |
                      >
                      >[/color]


                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: XML to DataSets

                        Lee,
                        Rather then use XSD.exe to explicitly create the typed dataset, I would
                        simply add the .XSD itself (the one created by DataSet.WriteXm lSchema) to my
                        project. I would then open the .XSD file, right click & select "Generate
                        DataSet".

                        This implicitly creates a typed dataset.

                        Hope this helps
                        Jay

                        "lgbjr" <lgbjr@online.n ospam> wrote in message
                        news:epIIXPuWFH A.2448@TK2MSFTN GP12.phx.gbl...
                        | Hi Jay,
                        |
                        | OK, so far, so good. Using your XSLT as a start, I added some more
                        | transforms (for things that where not included in the previous posts. So,
                        | the XSLT works, I can read the resulting XML file (dataset.ReadXM L), and I
                        | can create an XSD file (dataset.WriteX mlSchema). As a test, I imported the
                        | resulting Schema into Access, then imported the XML file (append data to
                        | exisiting tables) and everything looks good.
                        |
                        | After seeing that the results were good, I went back to my small transform
                        | app and added a shell to XSD.exe to generate a typed dataset from the XSD
                        | file.
                        |
                        | Now, my thinking is that using a typed data set is the easiest way to go
                        for
                        | use in a new app. I just add the typed dataset, and the rest is pretty
                        easy.
                        | But, of course, as soon as I realize something will be easy, it doesn't
                        | work!! XSD.exe creates a vb file that I added to a new app. then I dropped
                        a
                        | dataset on my form and selected the typed dataset, but I get an error
                        saying
                        | "Exception has been thrown by the target of an invocation". Not very
                        | descriptive!!
                        |
                        | Now, temporarily, forgetting the typed dataset, if I do a dataset.readxml ,
                        I
                        | have to dynamically create each table and column and relationship that I
                        | want to see in a grid. If I add the XSD file to my project, how can I use
                        | the tables/columns/relationships defined in the XSD file? Or can I only do
                        | this with a typed dataset created from the XSD file?
                        |
                        | TIA,
                        | Lee
                        |
                        <<snip>>


                        Comment

                        • lgbjr

                          #13
                          Re: XML to DataSets

                          Hi Jay,

                          Yep, that works! Quick question: I'm actually doing this project in 2003 and
                          2005 at the same time (just to see the differences. In 2005, if I
                          right-click on the XSD view, there isn't a "Generate Dataset" option.

                          Any ideas on how to proceed in 2005?

                          Thanks!

                          Lee

                          "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                          news:eH3LhruWFH A.3584@TK2MSFTN GP14.phx.gbl...[color=blue]
                          > Lee,
                          > Rather then use XSD.exe to explicitly create the typed dataset, I would
                          > simply add the .XSD itself (the one created by DataSet.WriteXm lSchema) to
                          > my
                          > project. I would then open the .XSD file, right click & select "Generate
                          > DataSet".
                          >
                          > This implicitly creates a typed dataset.
                          >
                          > Hope this helps
                          > Jay
                          >
                          > "lgbjr" <lgbjr@online.n ospam> wrote in message
                          > news:epIIXPuWFH A.2448@TK2MSFTN GP12.phx.gbl...
                          > | Hi Jay,
                          > |
                          > | OK, so far, so good. Using your XSLT as a start, I added some more
                          > | transforms (for things that where not included in the previous posts.
                          > So,
                          > | the XSLT works, I can read the resulting XML file (dataset.ReadXM L), and
                          > I
                          > | can create an XSD file (dataset.WriteX mlSchema). As a test, I imported
                          > the
                          > | resulting Schema into Access, then imported the XML file (append data to
                          > | exisiting tables) and everything looks good.
                          > |
                          > | After seeing that the results were good, I went back to my small
                          > transform
                          > | app and added a shell to XSD.exe to generate a typed dataset from the
                          > XSD
                          > | file.
                          > |
                          > | Now, my thinking is that using a typed data set is the easiest way to go
                          > for
                          > | use in a new app. I just add the typed dataset, and the rest is pretty
                          > easy.
                          > | But, of course, as soon as I realize something will be easy, it doesn't
                          > | work!! XSD.exe creates a vb file that I added to a new app. then I
                          > dropped
                          > a
                          > | dataset on my form and selected the typed dataset, but I get an error
                          > saying
                          > | "Exception has been thrown by the target of an invocation". Not very
                          > | descriptive!!
                          > |
                          > | Now, temporarily, forgetting the typed dataset, if I do a
                          > dataset.readxml ,
                          > I
                          > | have to dynamically create each table and column and relationship that I
                          > | want to see in a grid. If I add the XSD file to my project, how can I
                          > use
                          > | the tables/columns/relationships defined in the XSD file? Or can I only
                          > do
                          > | this with a typed dataset created from the XSD file?
                          > |
                          > | TIA,
                          > | Lee
                          > |
                          > <<snip>>
                          >
                          >[/color]


                          Comment

                          • lgbjr

                            #14
                            Re: XML to DataSets

                            HI Jay,

                            Actually I sent the last post a buick to quick. what I see in 2005 is that
                            the XSD file already looks like a dataset (meaning that when I view the XSD
                            file, the toolbox items that are available are listed under the heading of
                            "Dataset" and are items that one would add to a dataset, such as table
                            adapters, data tables, etc.). However, the project does not have a data
                            source. So, if I add a grid or some other control that I can bind to the
                            data, the datasource selection shows "None".

                            So, how do I get the XSD file in 2005 to show as a data source?

                            Thanks
                            Lee

                            "lgbjr" <lgbjr@online.n ospam> wrote in message
                            news:en2QsCvWFH A.3176@TK2MSFTN GP12.phx.gbl...[color=blue]
                            > Hi Jay,
                            >
                            > Yep, that works! Quick question: I'm actually doing this project in 2003
                            > and 2005 at the same time (just to see the differences. In 2005, if I
                            > right-click on the XSD view, there isn't a "Generate Dataset" option.
                            >
                            > Any ideas on how to proceed in 2005?
                            >
                            > Thanks!
                            >
                            > Lee
                            >
                            > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                            > news:eH3LhruWFH A.3584@TK2MSFTN GP14.phx.gbl...[color=green]
                            >> Lee,
                            >> Rather then use XSD.exe to explicitly create the typed dataset, I would
                            >> simply add the .XSD itself (the one created by DataSet.WriteXm lSchema) to
                            >> my
                            >> project. I would then open the .XSD file, right click & select "Generate
                            >> DataSet".
                            >>
                            >> This implicitly creates a typed dataset.
                            >>
                            >> Hope this helps
                            >> Jay
                            >>
                            >> "lgbjr" <lgbjr@online.n ospam> wrote in message
                            >> news:epIIXPuWFH A.2448@TK2MSFTN GP12.phx.gbl...
                            >> | Hi Jay,
                            >> |
                            >> | OK, so far, so good. Using your XSLT as a start, I added some more
                            >> | transforms (for things that where not included in the previous posts.
                            >> So,
                            >> | the XSLT works, I can read the resulting XML file (dataset.ReadXM L),
                            >> and I
                            >> | can create an XSD file (dataset.WriteX mlSchema). As a test, I imported
                            >> the
                            >> | resulting Schema into Access, then imported the XML file (append data
                            >> to
                            >> | exisiting tables) and everything looks good.
                            >> |
                            >> | After seeing that the results were good, I went back to my small
                            >> transform
                            >> | app and added a shell to XSD.exe to generate a typed dataset from the
                            >> XSD
                            >> | file.
                            >> |
                            >> | Now, my thinking is that using a typed data set is the easiest way to
                            >> go
                            >> for
                            >> | use in a new app. I just add the typed dataset, and the rest is pretty
                            >> easy.
                            >> | But, of course, as soon as I realize something will be easy, it doesn't
                            >> | work!! XSD.exe creates a vb file that I added to a new app. then I
                            >> dropped
                            >> a
                            >> | dataset on my form and selected the typed dataset, but I get an error
                            >> saying
                            >> | "Exception has been thrown by the target of an invocation". Not very
                            >> | descriptive!!
                            >> |
                            >> | Now, temporarily, forgetting the typed dataset, if I do a
                            >> dataset.readxml ,
                            >> I
                            >> | have to dynamically create each table and column and relationship that
                            >> I
                            >> | want to see in a grid. If I add the XSD file to my project, how can I
                            >> use
                            >> | the tables/columns/relationships defined in the XSD file? Or can I only
                            >> do
                            >> | this with a typed dataset created from the XSD file?
                            >> |
                            >> | TIA,
                            >> | Lee
                            >> |
                            >> <<snip>>
                            >>
                            >>[/color]
                            >
                            >[/color]


                            Comment

                            • Peter Huang [MSFT]

                              #15
                              Re: XML to DataSets

                              Hi

                              Since VS.NET 2005(Whidbey) has not been released, so far for Whidbey issue,
                              we have newsgroup about Whidbey.
                              microsoft.beta. whidbey.*

                              Thanks for your understanding!

                              If you still have any concern, please feel free to post here.

                              Best regards,

                              Peter Huang
                              Microsoft Online Partner Support

                              Get Secure! - www.microsoft.com/security
                              This posting is provided "AS IS" with no warranties, and confers no rights.

                              Comment

                              Working...