如何将 XML 读入与其 xsd 匹配的一个或多个类

发布于 2024-07-18 13:20:03 字数 1370 浏览 4 评论 0原文

所以我有一个 XSD 和一个以相同格式提供的 Web 服务。

现在我可以继续将 xml 读入文档,从类创建我的对象等等...但我在想,必须有一些更简单的方法来做到这一点。

我对吗? ;)

<ResultSet xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd">
 <Result precision="address">
  <Latitude>47.643727</Latitude>
  <Longitude>-122.130474</Longitude>
  <Address>1 Microsoft Way, #Way1</Address>
  <City>Redmond</City>
  <State>WA</State>
  <Zip>98052-6399</Zip>
  <Country>US</Country>
 </Result>
</ResultSet>

下面是自动生成的类(实际上是两个),使用 xsd.exe

类图

So I have an XSD and a webservice that delivers in that same format.

Now I could go ahead and read the xml into a document, create my objects from the class etc... But I am thinking, there must be some easier way to do that.

Am I right? ;)

<ResultSet xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd">
 <Result precision="address">
  <Latitude>47.643727</Latitude>
  <Longitude>-122.130474</Longitude>
  <Address>1 Microsoft Way, #Way1</Address>
  <City>Redmond</City>
  <State>WA</State>
  <Zip>98052-6399</Zip>
  <Country>US</Country>
 </Result>
</ResultSet>

Below are auto-generated classes (two actually), using xsd.exe

class diagram

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮萍、无处依 2024-07-25 13:20:03

您可以使用 XmlSerializer 反序列化 XML文本到由 xsd.exe 生成的类的实例中。
XmlSerializer 将使用放置在生成的类上的元数据属性在 XML 元素和 XML 元素之间来回映射。对象。

string xmlSource = "<ResultSet><Result precision=\"address\"><Latitude>47.643727</Latitude></Result></ResultSet>";

XmlSerializer serializer = new XmlSerializer(typeof(ResultSet));
ResultSet output;

using (StringReader reader = new StringReader(xmlSource))
{
    output = (ResultSet)serializer.Deserialize(reader);
}

You could use the XmlSerializer to deserialize the XML text into instances of the classes generated by xsd.exe.
The XmlSerializer will use the metadata attributes placed on the generated classes to map back and forth between XML elements and objects.

string xmlSource = "<ResultSet><Result precision=\"address\"><Latitude>47.643727</Latitude></Result></ResultSet>";

XmlSerializer serializer = new XmlSerializer(typeof(ResultSet));
ResultSet output;

using (StringReader reader = new StringReader(xmlSource))
{
    output = (ResultSet)serializer.Deserialize(reader);
}
白龙吟 2024-07-25 13:20:03

您只需从 XSD 创建一个类型化数据集,然后使用 XML 填充这些对象之一。 这是很常见的方法。

You could just create a Typed DataSet from the XSD and then fill one of those objects with the XML. That's the pretty common method.

书间行客 2024-07-25 13:20:03

Liquid XML Studio 中的 XSD 代码生成器 表现出色从 XML 模式创建高度兼容的 C# 或 vb.net 代码。 然后可以使用此代码来调用或实现 Web 服务。

如果您实现 Web 服务,那么您可以控制使用 XmlSchemaProvider 和 IXmlSerialized 生成的 WSDL,请参阅 控制您的 WSDL

The XSD Code Generator in Liquid XML Studio does a great job of creating highly compliant c# or vb.net code from an XML Schema. This code can then be used to call or implement a web service.

If your implementing a web service then you can take control of the WSDL produced using XmlSchemaProvider and IXmlSerializable, see Taking Control of your WSDL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文