XML document cannot contain multiple root level elements

后端 未结 4 570
萌比男神i
萌比男神i 2021-01-12 13:11

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


  J         


        
相关标签:
4条回答
  • 2021-01-12 13:20

    You just have to add root element will resolve 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>
    
    0 讨论(0)
  • 2021-01-12 13:24

    Assuming what you want to do is open the document anyway, you can set the ConformanceLevel of 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
    }
    
    0 讨论(0)
  • 2021-01-12 13:26
    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfTestClass>
      <testClass>
        <a>attr1</a>
      </testClass>
      <testClass>
        <a>attr2</a>
      </testClass>
    </ArrayOfTestClass>
    

    like this

    0 讨论(0)
  • 2021-01-12 13:37

    The XML document must have one and only one root element. You have to add root element. For instance,

    <?xml version="1.0" encoding="utf-8" ?> 
    <Employees>
        <Employee>
           .....
        </Employee>
        <Employee>
           ....
        </Employee>
    </Employees>
    
    0 讨论(0)
提交回复
热议问题