Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlException: Multiple document element was detected

Tags:

c#

xml

I'm reading a XML file in a very simple way:

XmlTextReader reader = new XmlTextReader(dataPath);

while(reader.Read()){ 
    switch (reader.Name){
        case "language":
            Debug.Log(reader.ReadString());
            break;
        case "file":
            Debug.Log(reader.ReadString());
            break;
        case "arg":
            Debug.Log(reader.ReadString());
            break;
    }

}

Where my xml is like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
 <config>
       <language>EN-US</language>
       <file>\File\Doc\sample.txt</file>
</config>
 <data>
       <arg>LKR</language>
</dara>

My first problem is this:

XmlException: Multiple document element was detected. file:///C:/prj/as/sample.xml Line 7, position 2.
Mono.Xml2.XmlTextReader.ReadStartTag ()
Mono.Xml2.XmlTextReader.ReadContent ()
Mono.Xml2.XmlTextReader.Read ()
System.Xml.XmlTextReader.Read ()
LectorXML.Start () (at as/sampleXML.cs:17)

And second, my output is language and file, but NO arg. Maybe because is a different node? How can i fix this?

like image 274
legami Avatar asked May 25 '26 17:05

legami


1 Answers

You can only have a single node element at the root of your document. You have a <config> and a <data>. Wrap them in a single document element:

<document>
  <config>
    <language>EN-US</language>
    <file>\File\Doc\sample.txt</file>
  </config>
  <data>
    <arg>LKR</arg>
  </data>
</document>
like image 148
drdwilcox Avatar answered May 27 '26 07:05

drdwilcox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!