【问题标题】:How to remove duplicate namespace attributes from nodes in xml?如何从 xml 中的节点中删除重复的命名空间属性?
【发布时间】:2012-07-20 19:21:07
【问题描述】:

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

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

下面是我的示例 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>

`

【问题讨论】:

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


    【解决方案1】:

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

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

    属性读取xmnls 而不是xmlns。正因为如此,xsi 命名空间前缀并没有按照您想象的方式定义。

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

    【讨论】:

    • 感谢更新。我做了更改,但仍然无法正常工作。知道如何删除重复的命名空间。
    猜你喜欢
    • 2014-08-20
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多