C# XML反序列化问题
我正在寻找一种使用C的方法,我可以将下面的XML反序列化为一个类。我可以用XmlDocument.LoadXml()读取它,但我想将它反序列化到对象中 我尝试对对象使用XmlSerializer:C# XML反序列化问题,c#,xml,C#,Xml,我正在寻找一种使用C的方法,我可以将下面的XML反序列化为一个类。我可以用XmlDocument.LoadXml()读取它,但我想将它反序列化到对象中 我尝试对对象使用XmlSerializer: [XmlRoot(ElementName = "properties", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata") ] public class MyDto { [XmlEleme
[XmlRoot(ElementName = "properties", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata") ]
public class MyDto
{
[XmlElement(ElementName = "ObjectID", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
public string ObjectID { get; set; }
public string ContactID { get; set; }
}
我的代码(内存流中充满了xml):
但是我弄错了
System.InvalidOperationException:XML文档(0,0)中存在错误。-->System.Xml.XmlException:缺少根元素
<?xml version="1.0" encoding="utf-8"?>
<Content type="application/xml">
<m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<d:ObjectID xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">920D2</d:ObjectID>
<d:ContactID xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">99999</d:ContactID>
</m:properties>
</Content>
920D2
99999
您的XmlSerializer
类对象完全错误,或者不适合您的XML
您可以从
试试下面的类对象
[XmlRoot("ObjectID", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
public class ObjectID
{
[XmlAttribute("d", Namespace = "http://www.w3.org/2000/xmlns/")]
public string D { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot("ContactID", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
public class ContactID
{
[XmlAttribute("d", Namespace = "http://www.w3.org/2000/xmlns/")]
public string D { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot("properties", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")]
public class Properties
{
[XmlElement("ObjectID", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
public ObjectID ObjectID { get; set; }
[XmlElement("ContactID", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
public ContactID ContactID { get; set; }
[XmlAttribute("m", Namespace = "http://www.w3.org/2000/xmlns/")]
public string M { get; set; }
}
[XmlRoot("Content")]
public class MyDto
{
[XmlElement("properties", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")]
public Properties Properties { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
}
输出:
我认为您的问题在于XML有一个根元素内容,XMLSerializer对此一无所知。错误表明memorystream没有与您发布的相同的XML文件,或者XML不是当前位置的根元素。在填充内存流之后,您必须在读取XmlReader之前将位置设置为零。谢谢,类对象是主要原因!
[XmlRoot("ObjectID", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
public class ObjectID
{
[XmlAttribute("d", Namespace = "http://www.w3.org/2000/xmlns/")]
public string D { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot("ContactID", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
public class ContactID
{
[XmlAttribute("d", Namespace = "http://www.w3.org/2000/xmlns/")]
public string D { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot("properties", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")]
public class Properties
{
[XmlElement("ObjectID", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
public ObjectID ObjectID { get; set; }
[XmlElement("ContactID", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
public ContactID ContactID { get; set; }
[XmlAttribute("m", Namespace = "http://www.w3.org/2000/xmlns/")]
public string M { get; set; }
}
[XmlRoot("Content")]
public class MyDto
{
[XmlElement("properties", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")]
public Properties Properties { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
}