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