Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML文档不能包含多个根级元素_C#_Xml - Fatal编程技术网

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

C# XML文档不能包含多个根级元素,c#,xml,C#,Xml,我有一个代码列表,其中有一个错误“XML文档不能包含多个根级别元素” 珍妮丝·理查森 财务主管 销售额 370-16-3631 单身 $4,500 吴艾伦 研究 385-22-3311 已婚的 $52,800 错误发生在第一个标记处。假设您要做的是无论如何打开文档,您可以将XmlReader的一致性级别设置为一致性级别.Fragment XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLeve

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


珍妮丝·理查森
财务主管
销售额
370-16-3631
单身
$4,500
吴艾伦
研究
385-22-3311
已婚的
$52,800

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

假设您要做的是无论如何打开文档,您可以将
XmlReader
一致性级别设置为
一致性级别.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
}

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

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

Janice Richardson财务主管
销售370-16-3631
单人4500美元
吴艾伦
研究385-22-3311
结婚52800美元

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

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

.....
....

属性1
属性2
像这样

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