从Web服务解析XML结果

时间:2022-10-29 20:33:57

I'm new to Web Services and XML and was tasked to parse an XML response packet returned.

我是Web Services和XML的新手,负责解析返回的XML响应数据包。

What's the best way to parse an XML result in C#.NET?

在C#.NET中解析XML结果的最佳方法是什么?

I need to bind to a data grid as the end result from a search query.

我需要绑定到数据网格作为搜索查询的最终结果。

7 个解决方案

#1


If you have access to the wsdl for the webservice, there is a utility wsdl that will generate the needed classes and deserialization to call a webservice and parse its response into those classes.

如果您可以访问web服务的wsdl,那么有一个实用程序wsdl将生成所需的类和反序列化以调用Web服务并将其响应解析为这些类。

Using the example webservice at w3schools, you would just run this command line:

使用w3schools上的示例webservice,您只需运行以下命令行:

wsdl "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL"

That would generate a file TempConvert.cs that you could add into your project. Then calling the webservice is just a matter of calling the generated functions:

这将生成一个可以添加到项目中的文件TempConvert.cs。然后调用webservice就是调用生成的函数:

Double fahrenheit = 32.2;
TempConvert tempConverter = new TempConvert();
string Celcius = tempConverter.FahrenheitToCelsius(fahrenheit.ToString());

That TempConvert class takes care of the details of building an XML query, contacting the webservice, and parsing the reply back in. This is a super-simple example, so you only get a string back, but since your webservice is likely to be a little more complicated, you'll get back more complicated classes containing the data you want in a parsed form. Depending on the complexity of the web service, this could even be as simple as an array of strings, or as complex as a giant class heirarchy.

TempConvert类负责构建XML查询,联系web服务以及解析回复的细节。这是一个非常简单的示例,因此您只能返回一个字符串,但由于您的Web服务很可能是稍微复杂一点,你会在解析的表单中找到包含你想要的数据的更复杂的类。根据Web服务的复杂性,这甚至可以像字符串数组一样简单,也可以像巨型类层次结构一样复杂。

#2


There is a fundamental thing you should know about XML parsing before deciding which technique is best.

在决定哪种技术最佳之前,您应该了解有关XML解析的基本知识。

With XMLDocument kind of objects the entire XML is loaded into memory where as with a reader object it is a stream, only the chunk that is being processed is loaded to memory. Obviously the reader uses little memory but will not give you the ability to go back/ ahead of the current node being processed. Whereas you have lot of freedom with the doc though the downside is there is a copy of the xml file in memory.

使用XMLDocument类对象,整个XML被加载到内存中,与读取器对象一样,它是一个流,只有正在处理的块被加载到内存中。显然,读者使用的内存很少,但是不能让你能够返回当前正在处理的节点。尽管你的文档有很多*,但缺点是内存中有一个xml文件的副本。

Accordingly if the size of xml is expected to be large, then XmlDocument will not be optimal. What kind of logic you apply using the xml will also affect the choice.

因此,如果预计xml的大小很大,那么XmlDocument将不是最佳的。使用xml应用什么样的逻辑也会影响选择。

#3


If dose not have a WSDL you can use the library found under

如果剂量没有WSDL,您可以使用下面的库

System.Xml

You can also use LINQ you can use the

你也可以使用LINQ你可以使用

System.Xml.Linq

Namespace for LINQ binding.

LINQ绑定的命名空间。

You will need to look at the packet and figure out what the scheme is.

您需要查看数据包并找出方案是什么。

#4


There are lots of different ways to do this, you could deserialize the xml into an object, use LINQ-to-XML to extract the data, load the XML into a DOM and xpath the data out, etc.

有很多不同的方法可以做到这一点,你可以将xml反序列化为一个对象,使用LINQ-to-XML来提取数据,将XML加载到DOM中,然后将数据输出xpath等。

Do you have a XSD for the response packet?

你有响应数据包的XSD吗?

#5


The most common way to parse XML in C# is to use the XMLDocument in System.XML.

在C#中解析XML的最常用方法是在System.XML中使用XMLDocument。

However, if the web service is SOAP-based, the easiest method to consume it is to add a reference to the service to your project and let the wizard generate the parsing code for you.

但是,如果Web服务是基于SOAP的,那么使用它的最简单方法是向项目添加对服务的引用,并让向导为您生成解析代码。

#6


add: using System.Xml

添加:使用System.Xml

As you peruse the structure of XML, you will see elements in each other (see object XmlElement and XmlNode). Each has child object collections (XmlNode.ChildNodes[]).

当您仔细阅读XML的结构时,您将看到彼此的元素(请参阅对象XmlElement和XmlNode)。每个都有子对象集合(XmlNode.ChildNodes [])。

XPath is a way to navigate to a specific node or subset of nodes. For a basic overview, read this article.

XPath是一种导航到特定节点或节点子集的方法。有关基本概述,请阅读本文。

#7


@LB: You have not mentioned what is the purpose of parsing the XML returned. If you are performing some modifications in the XML or querying it for some specific data, I would understand, but if you want to simply load it so that it can be displayed in a Datagrid, then I believe that you have two very straightforward (and conventionally used) options :

@LB:您还没有提到解析返回的XML的目的是什么。如果您在XML中执行某些修改或查询某些特定数据,我会理解,但如果您只想加载它以便它可以在Datagrid中显示,那么我相信您有两个非常简单(和常规使用)选项:

  • Directly bind the DataGrid to the XML, or use a XmlDataSource as an intermediary.
  • 直接将DataGrid绑定到XML,或使用XmlDataSource作为中介。

  • Load the XML into a DataSet and bind your DataGrid to that Dataset.
  • 将XML加载到DataSet中并将DataGrid绑定到该数据集。

Personally, I have never liked converting XML to Datasets, unless I have the time to create a specific schema for the XML document. So, I lean towards the first option.

就个人而言,我从未喜欢将XML转换为数据集,除非我有时间为XML文档创建特定的架构。所以,我倾向于第一个选择。

#1


If you have access to the wsdl for the webservice, there is a utility wsdl that will generate the needed classes and deserialization to call a webservice and parse its response into those classes.

如果您可以访问web服务的wsdl,那么有一个实用程序wsdl将生成所需的类和反序列化以调用Web服务并将其响应解析为这些类。

Using the example webservice at w3schools, you would just run this command line:

使用w3schools上的示例webservice,您只需运行以下命令行:

wsdl "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL"

That would generate a file TempConvert.cs that you could add into your project. Then calling the webservice is just a matter of calling the generated functions:

这将生成一个可以添加到项目中的文件TempConvert.cs。然后调用webservice就是调用生成的函数:

Double fahrenheit = 32.2;
TempConvert tempConverter = new TempConvert();
string Celcius = tempConverter.FahrenheitToCelsius(fahrenheit.ToString());

That TempConvert class takes care of the details of building an XML query, contacting the webservice, and parsing the reply back in. This is a super-simple example, so you only get a string back, but since your webservice is likely to be a little more complicated, you'll get back more complicated classes containing the data you want in a parsed form. Depending on the complexity of the web service, this could even be as simple as an array of strings, or as complex as a giant class heirarchy.

TempConvert类负责构建XML查询,联系web服务以及解析回复的细节。这是一个非常简单的示例,因此您只能返回一个字符串,但由于您的Web服务很可能是稍微复杂一点,你会在解析的表单中找到包含你想要的数据的更复杂的类。根据Web服务的复杂性,这甚至可以像字符串数组一样简单,也可以像巨型类层次结构一样复杂。

#2


There is a fundamental thing you should know about XML parsing before deciding which technique is best.

在决定哪种技术最佳之前,您应该了解有关XML解析的基本知识。

With XMLDocument kind of objects the entire XML is loaded into memory where as with a reader object it is a stream, only the chunk that is being processed is loaded to memory. Obviously the reader uses little memory but will not give you the ability to go back/ ahead of the current node being processed. Whereas you have lot of freedom with the doc though the downside is there is a copy of the xml file in memory.

使用XMLDocument类对象,整个XML被加载到内存中,与读取器对象一样,它是一个流,只有正在处理的块被加载到内存中。显然,读者使用的内存很少,但是不能让你能够返回当前正在处理的节点。尽管你的文档有很多*,但缺点是内存中有一个xml文件的副本。

Accordingly if the size of xml is expected to be large, then XmlDocument will not be optimal. What kind of logic you apply using the xml will also affect the choice.

因此,如果预计xml的大小很大,那么XmlDocument将不是最佳的。使用xml应用什么样的逻辑也会影响选择。

#3


If dose not have a WSDL you can use the library found under

如果剂量没有WSDL,您可以使用下面的库

System.Xml

You can also use LINQ you can use the

你也可以使用LINQ你可以使用

System.Xml.Linq

Namespace for LINQ binding.

LINQ绑定的命名空间。

You will need to look at the packet and figure out what the scheme is.

您需要查看数据包并找出方案是什么。

#4


There are lots of different ways to do this, you could deserialize the xml into an object, use LINQ-to-XML to extract the data, load the XML into a DOM and xpath the data out, etc.

有很多不同的方法可以做到这一点,你可以将xml反序列化为一个对象,使用LINQ-to-XML来提取数据,将XML加载到DOM中,然后将数据输出xpath等。

Do you have a XSD for the response packet?

你有响应数据包的XSD吗?

#5


The most common way to parse XML in C# is to use the XMLDocument in System.XML.

在C#中解析XML的最常用方法是在System.XML中使用XMLDocument。

However, if the web service is SOAP-based, the easiest method to consume it is to add a reference to the service to your project and let the wizard generate the parsing code for you.

但是,如果Web服务是基于SOAP的,那么使用它的最简单方法是向项目添加对服务的引用,并让向导为您生成解析代码。

#6


add: using System.Xml

添加:使用System.Xml

As you peruse the structure of XML, you will see elements in each other (see object XmlElement and XmlNode). Each has child object collections (XmlNode.ChildNodes[]).

当您仔细阅读XML的结构时,您将看到彼此的元素(请参阅对象XmlElement和XmlNode)。每个都有子对象集合(XmlNode.ChildNodes [])。

XPath is a way to navigate to a specific node or subset of nodes. For a basic overview, read this article.

XPath是一种导航到特定节点或节点子集的方法。有关基本概述,请阅读本文。

#7


@LB: You have not mentioned what is the purpose of parsing the XML returned. If you are performing some modifications in the XML or querying it for some specific data, I would understand, but if you want to simply load it so that it can be displayed in a Datagrid, then I believe that you have two very straightforward (and conventionally used) options :

@LB:您还没有提到解析返回的XML的目的是什么。如果您在XML中执行某些修改或查询某些特定数据,我会理解,但如果您只想加载它以便它可以在Datagrid中显示,那么我相信您有两个非常简单(和常规使用)选项:

  • Directly bind the DataGrid to the XML, or use a XmlDataSource as an intermediary.
  • 直接将DataGrid绑定到XML,或使用XmlDataSource作为中介。

  • Load the XML into a DataSet and bind your DataGrid to that Dataset.
  • 将XML加载到DataSet中并将DataGrid绑定到该数据集。

Personally, I have never liked converting XML to Datasets, unless I have the time to create a specific schema for the XML document. So, I lean towards the first option.

就个人而言,我从未喜欢将XML转换为数据集,除非我有时间为XML文档创建特定的架构。所以,我倾向于第一个选择。