数据集到XMLDocument重复三次

时间:2015-11-16 16:00:17

标签: xml vb.net linq

我正在尝试使用System.Xml.Linq将数据集中的值输出到xml文档,但是每个元素的值在每个数据集元素中重复三次,而不是只有一个

迭代数据集并转换xml我正在使用此代码

Dim variableList As New List(Of String)()
    Dim rowList As New List(Of String)()


    For Each column As DataColumn In ds.Tables(0).Columns
        variableList.Add(column.ColumnName)

        Dim variables As XElement = doc.Descendants().Where(Function(x) x.Name.LocalName = "variables").FirstOrDefault
        Dim variable As XElement = <variable varName=<%= column.ColumnName %> trait="textcontent" category="ns_flows;"></variable>
        variables.Add(variable)


        For Each dr As DataRow In ds.Tables(0).Rows
            rowList.Add(dr.Item(column))

            Dim sampleDataSets As XElement = doc.Descendants().Where(Function(x) x.Name.LocalName = "sampleDataSet").FirstOrDefault()
            Dim newEle As XElement = <<%= column.ColumnName %>><p><%= dr.Item(column) %></p></>
            sampleDataSets.Add(newEle)
        Next
    Next


    For Each dataRow As DataRow In ds.Tables(0).Rows
        Dim strDatasheet As String = dataRow("Dataset").ToString


        Dim sampleDataSets As XElement = doc.Descendants().Where(Function(x) x.Name.LocalName = "sampleDataSet").FirstOrDefault()
        Dim ns_v As XNamespace = sampleDataSets.GetNamespaceOfPrefix("v")

        Dim dataSetName As XAttribute = doc.Descendants().Where(Function(x) x.Name.LocalName = "sampleDataSet").FirstOrDefault().Attribute("dataSetName")
        dataSetName.SetValue(strDatasheet)

        Dim dataset As XElement = doc.Descendants().Where(Function(x) x.Name.LocalName = "Dataset").FirstOrDefault()
        Dim ns_dataset As XNamespace = dataset.Name.Namespace

        sampleDataSets.AddAfterSelf(sampleDataSets)

        Dim number As Integer
        number += 1
        If number = ds.Tables(0).Rows.Count Then
            sampleDataSets.Remove()
            dataSetName.Remove()
        End If
    Next

这将输出以下XMLDocument

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20001102//EN" "http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd"[
      <!ENTITY ns_graphs "http://ns.adobe.com/Graphs/1.0/">
      <!ENTITY ns_vars "http://ns.adobe.com/Variables/1.0/">
      <!ENTITY ns_imrep "http://ns.adobe.com/ImageReplacement/1.0/">
      <!ENTITY ns_custom "http://ns.adobe.com/GenericCustomNamespace/1.0/">
      <!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/">
      <!ENTITY ns_extend "http://ns.adobe.com/Extensibility/1.0/">
    ]>
    <svg>
      <variableSets xmlns="ns_vars;">
        <variableSet varSetName="binding1" locked="none">
          <variables>
            <variable varName="Dataset" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="Name" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="Rev" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="Desc" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="Marketing" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="Project" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="DPN" trait="textcontent" category="ns_flows;" xmlns=""></variable>
          </variables>
          <v:sampleDataSets xmlns:v="http://ns.adobe.com/Variables/1.0/" xmlns="http://ns.adobe.com/GenericCustomNamespace/1.0/">
            <v:sampleDataSet dataSetName="0FFHH A00_(Glasgow).ai">
              <Dataset xmlns="">
                <p>0FFHH A00_(Glasgow).ai</p>
              </Dataset>
              <Dataset xmlns="">
                <p>07FV6 A00_(Munster).ai</p>
              </Dataset>
              <Dataset xmlns="">
                <p>0C11F A00_(London).ai</p>
              </Dataset>
              <Name xmlns="">
                <p>0FFHH</p>
              </Name>
              <Name xmlns="">
                <p>0C11F</p>
              </Name>
              <Name xmlns="">
                <p>07FV6</p>
              </Name>
              <Rev xmlns="">
                <p>A00</p>
              </Rev>
              <Rev xmlns="">
                <p>A00</p>
              </Rev>
              <Rev xmlns="">
                <p>A00</p>
              </Rev>
              <Desc xmlns="">
                <p>ESPL</p>
              </Desc>
              <Desc xmlns="">
                <p>ESPL</p>
              </Desc>
              <Desc xmlns="">
                <p>ESPL</p>
              </Desc>
              <Marketing xmlns="">
                <p>E7270 </p>
              </Marketing>
              <Marketing xmlns="">
                <p>15011</p>
              </Marketing>
              <Marketing xmlns="">
                <p>15001</p>
              </Marketing>
              <Project xmlns="">
                <p>Creek 12</p>
              </Project>
              <Project xmlns="">
                <p>Creek 12 </p>
              </Project>
              <Project xmlns="">
                <p>Creek 12 </p>
              </Project>
              <DPN xmlns="">
                <p>DP/N: 1234</p>
              </DPN>
              <DPN xmlns="">
                <p>DP/N: 5345</p>
              </DPN>
              <DPN xmlns="">
                <p>DP/N: 35345</p>
              </DPN>
            </v:sampleDataSet>
            <v:sampleDataSet dataSetName="07FV6 A00_(Munster).ai">
              <Dataset xmlns="">
                <p>0FFHH A00_(Glasgow).ai</p>
              </Dataset>
              <Dataset xmlns="">
                <p>07FV6 A00_(Munster).ai</p>
              </Dataset>
              <Dataset xmlns="">
                <p>0C11F A00_(London).ai</p>
              </Dataset>
              <Name xmlns="">
                <p>0FFHH</p>
              </Name>
              <Name xmlns="">
                <p>0C11F</p>
              </Name>
              <Name xmlns="">
                <p>07FV6</p>
              </Name>
              <Rev xmlns="">
                <p>A00</p>
              </Rev>
              <Rev xmlns="">
                <p>A00</p>
              </Rev>
              <Rev xmlns="">
                <p>A00</p>
              </Rev>
              <Desc xmlns="">
                <p>ESPL</p>
              </Desc>
              <Desc xmlns="">
                <p>ESPL</p>
              </Desc>
              <Desc xmlns="">
                <p>ESPL</p>
              </Desc>
              <Marketing xmlns="">
                <p>15021</p>
              </Marketing>
              <Marketing xmlns="">
                <p>L15011</p>
              </Marketing>
              <Marketing xmlns="">
                <p> 15001</p>
              </Marketing>
              <Project xmlns="">
                <p>Creek 12 </p>
              </Project>
              <Project xmlns="">
                <p>Creek 12 </p>
              </Project>
              <Project xmlns="">
                <p>Creek 12 </p>
              </Project>
              <DPN xmlns="">
                <p>DP/N:456456</p>
              </DPN>
              <DPN xmlns="">
                <p>DP/N: 4564</p>
              </DPN>
              <DPN xmlns="">
                <p>DP/N: 456</p>
              </DPN>
            </v:sampleDataSet>
            <v:sampleDataSet dataSetName="0C11F A00_(London).ai">
              <Dataset xmlns="">
                <p>0FFHH A00_(Glasgow).ai</p>
              </Dataset>
              <Dataset xmlns="">
                <p>07FV6 A00_(Munster).ai</p>
              </Dataset>
              <Dataset xmlns="">
                <p>00C11F A00_(London).ai</p>
              </Dataset>
              <Name xmlns="">
                <p>0FFHH</p>
              </Name>
              <Name xmlns="">
                <p>0C11F</p>
              </Name>
              <Name xmlns="">
                <p>07FV6</p>
              </Name>
              <Rev xmlns="">
                <p>A00</p>
              </Rev>
              <Rev xmlns="">
                <p>A00</p>
              </Rev>
              <Rev xmlns="">
                <p>A00</p>
              </Rev>
              <Desc xmlns="">
                <p>ESPL</p>
              </Desc>
              <Desc xmlns="">
                <p>ESPL</p>
              </Desc>
              <Desc xmlns="">
                <p>ESPL</p>
              </Desc>
              <Marketing xmlns="">
                <p>15021</p>
              </Marketing>
              <Marketing xmlns="">
                <p>15011</p>
              </Marketing>
              <Marketing xmlns="">
                <p>15001</p>
              </Marketing>
              <Project xmlns="">
                <p>Creek 12 </p>
              </Project>
              <Project xmlns="">
                <p>Creek 12 </p>
              </Project>
              <Project xmlns="">
                <p> Creek 12 </p>
              </Project>
              <DPN xmlns="">
                <p>DP/N: 565465</p>
              </DPN>
              <DPN xmlns="">
                <p>DP/N: 6456</p>
              </DPN>
              <DPN xmlns="">
                <p>DP/N: 4564</p>
              </DPN>
            </v:sampleDataSet>
          </v:sampleDataSets>
        </variableSet>
      </variableSets>
    </svg>

在将下一个元素写入xml文档之前,代码似乎迭代了三次,是否有人能够提供关于我出错的地方的任何建议,文档应该看起来像以下格式。

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20001102//EN" "http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd"[
      <!ENTITY ns_graphs "http://ns.adobe.com/Graphs/1.0/">
      <!ENTITY ns_vars "http://ns.adobe.com/Variables/1.0/">
      <!ENTITY ns_imrep "http://ns.adobe.com/ImageReplacement/1.0/">
      <!ENTITY ns_custom "http://ns.adobe.com/GenericCustomNamespace/1.0/">
      <!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/">
      <!ENTITY ns_extend "http://ns.adobe.com/Extensibility/1.0/">
    ]>
    <svg>
      <variableSets xmlns="ns_vars;">
        <variableSet varSetName="binding1" locked="none">
          <variables>
            <variable varName="Dataset" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="Name" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="Rev" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="Desc" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="Marketing" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="Project" trait="textcontent" category="ns_flows;" xmlns=""></variable>
            <variable varName="DPN" trait="textcontent" category="ns_flows;" xmlns=""></variable>
          </variables>
          <v:sampleDataSets xmlns:v="http://ns.adobe.com/Variables/1.0/" xmlns="http://ns.adobe.com/GenericCustomNamespace/1.0/">
            <v:sampleDataSet dataSetName="0FFHH A00_(Glasgow).ai">
              <Dataset xmlns="">
                <p>0FFHH A00_(Glasgow).ai</p>
              </Dataset>          
              <Name xmlns="">
                <p>0FFHH</p>
              </Name>          
              <Rev xmlns="">
                <p>A00</p>
              </Rev>          
              <Desc xmlns="">
                <p>ESPL</p>
              </Desc> 
              <Marketing xmlns="">
                <p> E7270 </p>
              </Marketing>          
              <Project xmlns="">
                <p> Creek 12 </p>
              </Project>          
              <DPN xmlns="">
                <p>DP/N:3424</p>
              </DPN>
            </v:sampleDataSet>
            <v:sampleDataSet dataSetName=">07FV6 A00_(Munster).ai">          
              <Dataset xmlns="">
                <p>07FV6 A00_(Munster).ai</p>
              </Dataset>          
              <Name xmlns="">
                <p>07FV6</p>
              </Name>         
              <Rev xmlns="">
                <p>A00</p>
              </Rev>          
              <Desc xmlns="">
                <p>ESPL</p>
              </Desc>          
              <Marketing xmlns="">
                <p> E7270 </p>
              </Marketing>         
              <Project xmlns="">
                <p> Creek 12 </p>
              </Project>          
              <DPN xmlns="">
                <p>DP/N: 2167</p>
              </DPN>
            </v:sampleDataSet>
            <v:sampleDataSet dataSetName="0C11F A00_(London).ai">          
              <Dataset xmlns="">
                <p>0C11F A00_(London).ai</p>
              </Dataset>          
              <Name xmlns="">
                <p>07FV6</p>
              </Name>         
              <Rev xmlns="">
                <p>A00</p>
              </Rev>          
              <Desc xmlns="">
                <p>ESPL,</p>
              </Desc>          
              <Marketing xmlns="">
                <p> E7270 </p>
              </Marketing>         
              <Project xmlns="">
                <p> Creek 12 </p>
              </Project>          
              <DPN xmlns="">
                <p>DP/N:65756</p>
              </DPN>
            </v:sampleDataSet>                
        </variableSet>
      </variableSets>
    </svg>

更新 Jdweng的回答是正确的,我在下面显示正确的代码只迭代数据行一次。

For Each dataRow As DataRow In result.Tables(0).Rows
        If Not dataRow Is Nothing Then

            Dim strDatasheet As String = dataRow(variableList.Item(0)).ToString

            Dim sampleDataSets As XElement = doc.Descendants().Where(Function(x) x.Name.LocalName = "sampleDataSet").LastOrDefault
            Dim dataSetName As XAttribute = doc.Descendants().Where(Function(x) x.Name.LocalName = "sampleDataSet").LastOrDefault.Attribute("dataSetName")
            dataSetName.SetValue(strDatasheet)

            sampleDataSets.AddAfterSelf(sampleDataSets)

            For Each item In variableList
                Dim ns_v1 As XNamespace = sampleDataSets.GetDefaultNamespace()
                Dim itemElement As XElement = New XElement(ns_v1 + item)
                Dim dataEle As XElement = New XElement(<p><%= dataRow(item).ToString %></p>)
                sampleDataSets.Add(itemElement)
                itemElement.Add(dataEle)


                For Each node As XElement In sampleDataSets.Descendants()
                    If node.Name.NamespaceName = "" Then
                        node.Attributes("xmlns").Remove()
                        node.Name = node.Parent.Name.[Namespace] + node.Name.LocalName
                    End If
                Next
            Next
        End If
    Next

1 个答案:

答案 0 :(得分:0)

答案是只迭代数据行一次而不是三次。 下面我展示了代码。

For Each dataRow As DataRow In result.Tables(0).Rows
    If Not dataRow Is Nothing Then

        Dim strDatasheet As String = dataRow(variableList.Item(0)).ToString

        Dim sampleDataSets As XElement = doc.Descendants().Where(Function(x) x.Name.LocalName = "sampleDataSet").LastOrDefault
        Dim dataSetName As XAttribute = doc.Descendants().Where(Function(x) x.Name.LocalName = "sampleDataSet").LastOrDefault.Attribute("dataSetName")
        dataSetName.SetValue(strDatasheet)

        sampleDataSets.AddAfterSelf(sampleDataSets)

        For Each item In variableList
            Dim ns_v1 As XNamespace = sampleDataSets.GetDefaultNamespace()
            Dim itemElement As XElement = New XElement(ns_v1 + item)
            Dim dataEle As XElement = New XElement(<p><%= dataRow(item).ToString %></p>)
            sampleDataSets.Add(itemElement)
            itemElement.Add(dataEle)


            For Each node As XElement In sampleDataSets.Descendants()
                If node.Name.NamespaceName = "" Then
                    node.Attributes("xmlns").Remove()
                    node.Name = node.Parent.Name.[Namespace] + node.Name.LocalName
                End If
            Next
        Next
    End If
Next