将 xml 反序列化为 c# 对象时,XML 文档 (2, 2) 出现错误
Posted
技术标签:
【中文标题】将 xml 反序列化为 c# 对象时,XML 文档 (2, 2) 出现错误【英文标题】:There is an error in XML document (2, 2) when desearlizing xml to c# object 【发布时间】:2020-08-13 03:35:44 【问题描述】:XML
<?xml version="1.0" encoding="UTF-8"?>
<orgc:Organizations xmlns:orgc="urn:workday.com/connector/orgs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<orgc:Organization>
<orgc:Organization_ID>SR1Code34</orgc:Organization_ID>
<orgc:Organization_Code>SR1Code34</orgc:Organization_Code>
<orgc:Organization_Type>Cost_Center_Hierarchy</orgc:Organization_Type>
<orgc:Organization_Name>LTL Services</orgc:Organization_Name>
<orgc:Organization_Description>LTL Services</orgc:Organization_Description>
<orgc:Organization_Subtype>ORGANIZATION_SUBTYPE-3-20</orgc:Organization_Subtype>
<orgc:Inactive>false</orgc:Inactive>
<orgc:Superior_Organization>DL2Code11</orgc:Superior_Organization>
</orgc:Organization>
<orgc:Organization>
<orgc:Organization_ID>SR1Code35</orgc:Organization_ID>
<orgc:Organization_Code>SR1Code35</orgc:Organization_Code>
<orgc:Organization_Type>Cost_Center_Hierarchy</orgc:Organization_Type>
<orgc:Organization_Name>Consolidation</orgc:Organization_Name>
<orgc:Organization_Description>Consolidation</orgc:Organization_Description>
<orgc:Organization_Subtype>ORGANIZATION_SUBTYPE-3-20</orgc:Organization_Subtype>
<orgc:Inactive>false</orgc:Inactive>
<orgc:Superior_Organization>DL2Code11</orgc:Superior_Organization>
</orgc:Organization>
</orgc:Organizations>
类
[XmlRoot(ElementName = "Organizations", Namespace = "urn: workday.com/connector/orgs", IsNullable = true )]
public class CostCenterHierarchy
[XmlElement("orgc:Organization_ID")]
public string CostCenterHierarchyId get; set;
[XmlElement("orgc:Organization_Code")]
public string Code get; set;
[XmlElement("orgc:Organization_Name")]
public string Name get; set;
[XmlElement("orgc:Organization_Description")]
public string Description get; set;
[XmlElement("orgc:Organization_Subtype")]
public string Subtype get; set;
[XmlElement("orgc:Superior_Organization")]
public string ParentHierarchyId get; set;
将xml反序列化为c#类的方法
private List<CostCenterHierarchy> ProcessCostCenterHierarchy(string filePath)
var costCenterHierarchyList = new List<CostCenterHierarchy>();
//var costCenterHierarchy = new CostCenterHierarchy();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<CostCenterHierarchy>));
using (var reader = XmlReader.Create(filePath))
var test = xmlSerializer.Deserialize(reader);
return costCenterHierarchyList;
错误信息
Message = "There is an error in XML document (2, 2)."
InnerException = "<Organizations xmlns='urn:workday.com/connector/orgs'> was not expected."
我不确定我哪里出错了。似乎它应该很容易,但我已经玩过这个并不断收到相同的错误消息。任何帮助将非常感激。
【问题讨论】:
您的Namespace
属性包含一个不在文档中的空格(在urn:
之后)。 也许这就是问题所在? (您是否尝试seralizing List<CostCenterHierarchy>
以查看结果如何?)
【参考方案1】:
下面的代码有效。您有一个数组,并且序列化不喜欢列表作为类型。 serializaer 不喜欢的 Url "urn:workday.com/connector/orgs" 必须将 "urn:" 替换为 "http://workday.com/connector/orgs"。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
class Program
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Organizations));
Organizations organizations = (Organizations)serializer.Deserialize(reader);
[XmlRoot(ElementName = "Organizations", Namespace = "http://workday.com/connector/orgs")]
public class Organizations
[XmlElement(ElementName = "Organization", Namespace = "http://workday.com/connector/orgs")]
public List<CostCenterHierarchy> organizations get; set;
public class CostCenterHierarchy
[XmlElement("Organization_ID")]
public string CostCenterHierarchyId get; set;
[XmlElement("Organization_Code")]
public string Code get; set;
[XmlElement("Organization_Name")]
public string Name get; set;
[XmlElement("Organization_Description")]
public string Description get; set;
[XmlElement("Organization_Subtype")]
public string Subtype get; set;
[XmlElement("Superior_Organization")]
public string ParentHierarchyId get; set;
【讨论】:
以上是关于将 xml 反序列化为 c# 对象时,XML 文档 (2, 2) 出现错误的主要内容,如果未能解决你的问题,请参考以下文章
XML 文档中存在错误 (0, 0) - 将 XML 反序列化为对象时出错