使用已知的 XSD 从 XML 读取类型化对象
我有以下(作为示例)XML 文件和 XSD。
<?xml version="1.0" encoding="utf-8" ?>
<foo>
<DateVal>2010-02-18T01:02:03</DateVal>
<TimeVal>PT10H5M3S</TimeVal>
</foo>
和
version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="DateVal" type="xs:dateTime" />
<xs:element name="TimeVal" type="xs:duration" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
然后是以下 C# 代码:
static void Main(string[] args)
{
XmlDocument xd = new XmlDocument();
XmlSchema xs;
using (var fs = File.OpenRead(FilePath + "SimpleFields.xsd"))
{
xs = XmlSchema.Read(fs, null);
}
xd.Schemas.Add(xs);
xd.Load((FilePath + "SimpleFields.xml"));
xd.Validate(null);
var el_root = xd.DocumentElement;
var el_date = (XmlElement)el_root.SelectSingleNode("./DateVal");
//WANTED: el_date.Value = 2010-02-18 01:02:03 (as a DateTime Object)
//ACTUAL: el_date.InnerText="2010-02-18T01:02:03"
var el_duration = (XmlElement)el_root.SelectSingleNode("./TimeVal");
//WANTED: el_date.Value = 10 hours, 5 minutes, 3 seconds (as a TimeSpan Object)
//ACTUAL: el_date.InnerText="PT10H5M3S"
Console.WriteLine("DONE");
Console.ReadLine();
}
如何将数据读取为强类型对象?
我将针对 WindowsMobile 设备,但这不会对答案产生太大影响。 (可以是.NET 2.0或3.5...不确定Sstem.Xml.Linq是否有帮助)
I have the following (as an example) XML file and XSD.
<?xml version="1.0" encoding="utf-8" ?>
<foo>
<DateVal>2010-02-18T01:02:03</DateVal>
<TimeVal>PT10H5M3S</TimeVal>
</foo>
and
version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="DateVal" type="xs:dateTime" />
<xs:element name="TimeVal" type="xs:duration" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Then the following C# code:
static void Main(string[] args)
{
XmlDocument xd = new XmlDocument();
XmlSchema xs;
using (var fs = File.OpenRead(FilePath + "SimpleFields.xsd"))
{
xs = XmlSchema.Read(fs, null);
}
xd.Schemas.Add(xs);
xd.Load((FilePath + "SimpleFields.xml"));
xd.Validate(null);
var el_root = xd.DocumentElement;
var el_date = (XmlElement)el_root.SelectSingleNode("./DateVal");
//WANTED: el_date.Value = 2010-02-18 01:02:03 (as a DateTime Object)
//ACTUAL: el_date.InnerText="2010-02-18T01:02:03"
var el_duration = (XmlElement)el_root.SelectSingleNode("./TimeVal");
//WANTED: el_date.Value = 10 hours, 5 minutes, 3 seconds (as a TimeSpan Object)
//ACTUAL: el_date.InnerText="PT10H5M3S"
Console.WriteLine("DONE");
Console.ReadLine();
}
How can I read the data as strongly typed objects ?
I will be targetting a WindowsMobile device, but this shouldn't need to affect the answer too much. (can be .NET 2.0 or 3.5 ... Not sure if Sstem.Xml.Linq will help or not)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要执行两个步骤:
1) 获取 XML 架构文件并通过
xsd.exe
实用程序运行它(该实用程序随 Windows SDK 一起提供 - 位于C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\
或一些类似的路径这可以将 XSD 文件转换为 C# 类:这应该为您提供一个包含类的文件
yourfile.cs
。 2) 现在,有了该C# 类,您应该能够将 XML 文件反序列化为新对象的实例:
这就是最简单的了! :-)
You need to do two steps:
1) Take your XML schema file and run it through the
xsd.exe
utility (which comes with the Windows SDK - it's inC:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\
or some similar path. This can turn the XSD file into a C# class:This should give you a file
yourfile.cs
which contains a class representing that XML schema.2) Now, armed with that C# class, you should be able to just deserializing the XML file into an instance of your new object:
That's about as simple as it gets! :-)
好的 - 找到了我正在寻找的答案。
它是 XmlConvert 类。
就读取整个强类型类而言,马克的答案是正确的,但在这种情况下,我只想读取单个强类型元素/节点。
OK - Found the answer I was looking for.
it is the XmlConvert class.
Marc's answer was correct in terms of reading in a whole strongly-typed class, but in this case I only wanted to read a single strongly-typed element/node.