0

我有两个巨大的 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>

`
4

1 回答 1

0

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

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

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

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

于 2012-07-20T20:11:45.947 回答