如何从xml中的节点中删除重复的命名空间属性?

时间:2012-07-20 19:21:07

标签: xml vb.net xml-parsing xmlreader xmlwriter

我有两个巨大的1 gb xml文件。它们具有相同的结构。我正在尝试合并它们。 脚本使用xmltextreader和xmltextwriter.it工作正常,除了它将命名空间复制到多个节点。我阅读了很多博客和文档但却找不到合适的解决方案。 任何想法或帮助都非常适合。

对于测试puspose,我只是从xml下面读取并写入新的xml文件。 在输出文件中,tittle节点有这个额外的命名空间,我不想要。

以下是我的示例xml文件。

<?xml version="1.0" encoding="utf-8"?>
<records xmnls:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sample.xsd">
<record category="xyz" editor="" entered="sdsd" sub-category="sds" uid="ds" updated="sd-07-15">
    <person ssn="" e-i="M">
      <title xsi:nil="true"/>
      <position>abcd</position>
      <names>
        <first_name>xyz</first_name>
        <last_name>xyz</last_name>
      </names>
    </person>
</record>
<record category="xyz" editor="" entered="sdsd" sub-category="sds" uid="ds" updated="sd-07-15">
    <person ssn="" e-i="M">
      <title xsi:nil="true"/>
      <position>abcd</position>
      <names>
        <first_name>xyz</first_name>
        <last_name>xyz</last_name>
      </names>
    </person>
</record>
</records>

my code is as below

        Public Sub Main()
        Dim DownloadPEPLocation As String = Dts.Variables("xyz").Value
        Dim ACTIMIZESource As String = Dts.Variables("ACTIMIZESource").Value
        Dim PEPTextReader As Xml.XmlTextReader
        Dim Destination As Xml.XmlTextWriter
        Destination = New Xml.XmlTextWriter(ACTIMIZESource, System.Text.Encoding.UTF8)
        Destination.Formatting = Formatting.Indented
        Destination.Namespaces = True

        PEPTextReader = New XmlTextReader(DownloadPEPLocation)
        PEPTextReader.WhitespaceHandling = WhitespaceHandling.None

        Destination.WriteStartDocument()
        Destination.WriteStartElement("records")

        Destination.WriteAttributeString("xmnls:xsi", "http://www.w3.org/2001/XMLSchema-instance")
        Destination.WriteAttributeString("xsi:noNamespaceSchemaLocation", "world-check.xsd")

        Dim PEPreading As Boolean = PEPTextReader.Read()
        Do While (PEPreading)
            If (PEPTextReader.NodeType = XmlNodeType.Element And PEPTextReader.LocalName = "record") Then
                Destination.WriteNode(PEPTextReader, True)
                Destination.Flush()
            Else
                PEPreading = PEPTextReader.Read()
            End If
        Loop

        Destination.WriteEndElement()
        Destination.WriteEndDocument()
        Destination.Close()
        PEPTextReader.Close()


Output is look like this.

<?xml version="1.0" encoding="utf-8"?>
<records xmnls:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sample.xsd">
<record category="xyz" editor="" entered="sdsd" sub-category="sds" uid="ds" updated="sd-07-15">
    <person ssn="" e-i="M">
      <title xsi:nil="true" **xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"** />
      <position>abcd</position>
      <names>
        <first_name>xyz</first_name>
        <last_name>xyz</last_name>
      </names>
    </person>
</record>
<record category="xyz" editor="" entered="sdsd" sub-category="sds" uid="ds" updated="sd-07-15">
    <person ssn="" e-i="M">
      <title xsi:nil="true" **xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"** />
      <position>abcd</position>
      <names>
        <first_name>xyz</first_name>
        <last_name>xyz</last_name>
      </names>
    </person>
</record>
</records>

`

1 个答案:

答案 0 :(得分:0)

@Tapan:根据您的输入和输出示例,似乎在xmlns根元素的<records>属性上无意中转换了两个字母:

<records xmnls:xsi="http://www.w3.org/2001/XMLSchema-instance"
         ^^^^^

该属性显示的是xmnls,而不是xmlns。因此,xsi命名空间前缀未按您认为的那样定义。

尝试在输入文件中进行此更改,以查看输出文件中明显多余的xsi属性是否消失。

相关问题