0

I have two huge 1 gb xml files.both has same structure.I am trying to merge them. script use xmltextreader and xmltextwriter.it works fine except it duplicating namespace to multiple nodes. I read so many blogs and documents but couln't find proper solution. Any idea or help is really appriciated.

For test puspose i am just reading from below xml and writing to new xml file. In output file tittle node has this extra namespace which i dont want.

Below is my sample xml file.

<?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 Answers1

0

@Tapan: Based on your input and output examples, it appears that two letters have been inadvertantly transposed on the xmlns attribute in the <records> root element:

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

The attribute reads xmnls instead of xmlns. Because of this, the xsi namespace prefix is not being defined the way you think it is.

Try making this change in the input file to see if the apparently redundant xsi attributes in the output file go away.

DavidRR
  • 18,291
  • 25
  • 109
  • 191