XML 文档不能包含多个根级元素

发布于 2024-12-15 23:15:35 字数 765 浏览 0 评论 0原文

我有一个代码列表,其中出现错误“XML 文档不能包含多个根级别元素”

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

该错误发生在第一个 标记处。

I have a list of codes to which i have an error "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 at the first <Employee> tag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

維他命╮ 2024-12-22 23:15:36

XML 文档必须有一个且只有一个根元素。您必须添加根元素。例如,

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

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>
鲜血染红嫁衣 2024-12-22 23:15:36

假设您想要打开文档,则可以将 XmlReaderConformanceLevel 设置为 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
}

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
}
等待圉鍢 2024-12-22 23:15:36

您只需添加根元素即可解决您的错误......

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

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>
﹂绝世的画 2024-12-22 23:15:36
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfTestClass>
  <testClass>
    <a>attr1</a>
  </testClass>
  <testClass>
    <a>attr2</a>
  </testClass>
</ArrayOfTestClass>

像这样

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfTestClass>
  <testClass>
    <a>attr1</a>
  </testClass>
  <testClass>
    <a>attr2</a>
  </testClass>
</ArrayOfTestClass>

like this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文