XML document cannot contain multiple root level elements

I have a list of codes to which I have an error: "An XML document cannot contain multiple root level elements"

<Employee> <Name ID= "JanRich">Janice Richardson</Name> <Role>Finance Supervisor</Role> <Department>Sales</Department> <CPF_Number>370-16-3631</CPF_Number> <Marital_Status>Single</Marital_Status> <Salary>$4,500</Salary> </Employee> <Employee> <Name ID= 'AlanWu'>Alan Wu</Name> <Role></Role> <Department>Research</Department> <CPF_Number> 385-22-3311 </CPF_Number> <Marital_status>Married</Marital_status> <Salary>$52,800</Salary> </Employee> 

The error occurs when the first <Employee> .

+7
source share
4 answers

The must XML document has one and only one root element. You must add the root element. For example,

 <?xml version="1.0" encoding="utf-8" ?> <Employees> <Employee> ..... </Employee> <Employee> .... </Employee> </Employees> 
+10
source

Assuming what you want to do is still open the document, you can set the ConformanceLevel in the XmlReader to ConformanceLevel.Fragment .

 XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; // input is a stream or filename using (XmlReader reader = XmlReader.Create(input, settings)) { // use the reader } 
+3
source

You just need to add the root element to resolve the ur ........ error

  <root> <Employee> <Name ID= "JanRich">Janice Richardson</Name> <Role>Finance Supervisor</Role> <Department>Sales</Department> <CPF_Number>370-16-3631</CPF_Number> <Marital_Status>Single</Marital_Status> <Salary>$4,500</Salary> </Employee> <Employee> <Name ID= 'AlanWu'>Alan Wu</Name> <Role></Role> <Department>Research</Department> <CPF_Number> 385-22-3311 </CPF_Number> <Marital_status>Married</Marital_status> <Salary>$52,800</Salary> </Employee> </root> 
+2
source
 <?xml version="1.0" encoding="utf-8"?> <ArrayOfTestClass> <testClass> <a>attr1</a> </testClass> <testClass> <a>attr2</a> </testClass> </ArrayOfTestClass> 

like this

0
source

All Articles