XML nodes not appearing as children of parent

时间:2015-07-28 16:29:17

标签: xml vb.net

I'm really having trouble on something that I think should be fairly easy to accomplish.

I have an Excel file that is read into a dataset (called "ds" in my program). The file contains part data that needs to be loaded into a source XML file with the following format (and sample data):

<dl:Data>
  <dl:Location>Site01</dl:Location>
  <dl:LogisticsEvent>
    <dl:DateTime>01-JUL-2015 12:00:00 PM</dl:DateTime>
    <dl:AssemblyItem>
      <dl:ProductName>Test Part1</dl:ProductName>
      <dl:ProductNumber>998741</dl:ProductNumber>
      <dl:ProductDash>050</dl:ProductDash>
      <dl:SerialNumber>LX2247</dl:SerialNumber>
      <dl:ProductRevision>R</dl:ProductRevision>
    </dl:AssemblyItem>
    <dl:Activity>Stat Change</dl:Activity>
    <dl:Condition>Good</dl:Condition>
    <dl:Usage>Production</dl:Usage>
  </dl:LogisticsEvent>
</dl:Data>

I loop through the excel data and load it into a new dataset (called "ds2" in my program), write it to XML, and then an XMLDocument.

The problem I'm having is that when I view the XML, the data in the AssemblyItem tag is not appearing as a child node of the LogisticsEvent tag and the data in that tag is not appearing as a child of the Data tag. Basically, the Assembly tag & everything else in the LogisticsEvent tag appear as their own parent nodes (instead as in the sample above).

Here is the code I used to loop through and create the XML:

    Dim dsRow As Integer = 0
    Dim gridRow As Integer = 0


    For Each record In ds.Tables(0).Rows

        ds2.Tables(4).Rows.Add()
        ds2.Tables(4).Rows(dsRow).Item("ProductName") = ds.Tables(0).Rows(gridRow).Item("ProductName").ToString
        ds2.Tables(4).Rows(dsRow).Item("ProductNumber") = ds.Tables(0).Rows(gridRow).Item("ProductNumber").ToString
        ds2.Tables(4).Rows(dsRow).Item("ProductDash") = ds.Tables(0).Rows(gridRow).Item("ProductDash").ToString
        ds2.Tables(4).Rows(dsRow).Item("SerialNumber") = ds.Tables(0).Rows(gridRow).Item("SerialNumber").ToString
        ds2.Tables(4).Rows(dsRow).Item("ProductRevision") = ds.Tables(0).Rows(gridRow).Item("ProductRevision").ToString

        ds2.Tables(3).Rows.Add()
        ds2.Tables(3).Rows(dsRow).Item("Activity") = ds.Tables(0).Rows(gridRow).Item("Activity").ToString
        ds2.Tables(3).Rows(dsRow).Item("Condition") = ds.Tables(0).Rows(gridRow).Item("Condition").ToString
        ds2.Tables(3).Rows(dsRow).Item("Usage") = ds.Tables(0).Rows(gridRow).Item("Usage").ToString

        dsRow += 1
        gridRow += 1

    Next

    ds2.AcceptChanges()
    ds2.WriteXml(filePathFull)

    Dim xDoc As New XmlDocument
    Dim schemaLocation As XmlAttribute = Nothing
    Dim schemaString As String = "http://www.w3.org/2001/XMLSchema-instance"
    Dim schemaValue As String = "http://www.tgin.org/trident/7143706-001 7143706-001RevA.xsd"


    xDoc.Load(filePathFull)
    schemaLocation = xDoc.CreateAttribute("xsi", "schemaLocation", schemaString)
    schemaLocation.Value = schemaValue
    xDoc.DocumentElement.SetAttributeNode(schemaLocation)
    xDoc.Save(filePathFull)

On the first loop, the data appears correctly in the XML. It's on the subsequent loops that XML gets messed up. Any help would be greatly appreciated. Thank you very much.

1 个答案:

答案 0 :(得分:0)

试试这个

const