我需要将以下XML反序列化为C#
XML:
<rdlt version="1.5" xmlns:rdlt="http://www.rdlt.org" xmlns="http://www.rdlt.org">
<created>2016-02-18T15:36:29.326+00:00</created>
<updated>2016-02-18T15:36:29.326+00:00</updated>
</rdlt>
对象:
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "http://www.rdml.org")]
public class Container
{
[XmlElement(Namespace = "http://www.rdml.org")]
public DateTime created { get; set; }
[XmlElement(Namespace = "http://www.rdml.org")]
public DateTime updated { get; set; }
}
反序列化:
var reader = XmlReader.Create(@"C:\tmp\test.xml");
var serializer = new DataContractSerializer(typeof(Container));
var object = serializer.ReadObject(reader);
我收到以下错误,表明元素和架构不匹配:
Additional information: Error in line 1 position 2. Expecting element
'Container' from namespace 'http://schemas.datacontract.org/2004/07/ConsoleApplication1'..
Encountered 'Element' with name 'rdlt', namespace 'http://www.rdlt.org'.
已经尝试过
答案 0 :(得分:1)
试试这个...这段代码使用XmlSerializer .....
Usings .....
action [:create]
类.....
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
代码.....
[XmlRoot(ElementName = "rdlt", Namespace = "http://www.rdlt.org")]
public class Container
{
[XmlElement(ElementName = "created", Namespace = "http://www.rdlt.org")]
public string Created { get; set; }
[XmlElement(ElementName = "updated", Namespace = "http://www.rdlt.org")]
public string Updated { get; set; }
}
您的XML
class Program
{
static void Main(string[] args)
{
string strXML = File.ReadAllText("xml.xml");
byte[] bufXML = ASCIIEncoding.UTF8.GetBytes(strXML);
MemoryStream ms1 = new MemoryStream(bufXML);
// Deserialize to object
XmlSerializer serializer = new XmlSerializer(typeof(Container));
try
{
using (XmlReader reader = new XmlTextReader(ms1))
{
Container deserializedXML = (Container)serializer.Deserialize(reader);
}// put a break point here and mouse-over deserializedXML ….
}
catch (Exception ex)
{
throw;
}
}
}
我正在从名为xml.xml的应用程序构建文件夹中的文件中读取XML到一个字符串...您需要从其他地方获取XML字符串或创建xml.xml文件并保存XML上面的代码工作
答案 1 :(得分:0)
有几个问题:
object
不是本地变量的有效名称(请确保下次使用真实代码),它可以是@object
; ?xml
标记开头(例如<?xml version="1.0" ?>
); version
是?xml
的一部分,如果您想在根元素中使用 xml属性 version="1.5"
,那么您就遇到了麻烦; 如果您必须反序列化那个确切的xml
,那么您唯一的选择就是手动反序列化,例如:使用linq-to-xml:
var xml = XDocument.Load(@"C:\tmp\test.xml");
string version = xml.Root.Attribute("version").Value;
string space = "http://www.rdlt.org";
var obj = new Container()
{
created = DateTime.Parse(xml.Root.Element(XName.Get("created", space)).Value),
updated = DateTime.Parse(xml.Root.Element(XName.Get("updated", space)).Value),
};
如果您省略version
并且可以添加?xml
代码,则DataSerializer
应该有效。
E.g。这个有效 xml:
<?xml version="1.0" encoding="utf-8"?>
<rdlt xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.rdml.org">
<created>0001-01-01T00:00:00</created>
<updated>0001-01-01T00:00:00</updated>
</rdlt>
可以使用以下命令反序列化:
[DataContract(Name = "rdlt", Namespace = "http://www.rdml.org")]
public class Container
{
[DataMember]
public DateTime created { get; set; }
[DataMember]
public DateTime updated { get; set; }
}
// deserialize (using your code!)
var reader = XmlReader.Create(@"C:\tmp\test.xml");
var serializer = new DataContractSerializer(typeof(Container));
var obj = serializer.ReadObject(reader);