如何将XML元素值保存到变量

时间:2021-04-27 13:32:37

Part of my MVC app is a controller action method that POSTs data to a web service and receives an XML response. I have figured out how to get the response into an XDocument but now I need to go through the info and get some of the node values. I have been researching, and the closest option I can find it to use XPath.

我的MVC应用程序的一部分是一个控制器操作方法,它将数据POST到Web服务并接收XML响应。我已经想出如何将响应放到XDocument中,但现在我需要查看信息并获取一些节点值。我一直在研究,最接近我可以找到使用XPath的选项。

I am wondering if this is the best way to go, or if you suggest another way. The XML I am working with will look something like:

我想知道这是否是最佳方式,或者你是否建议采用其他方式。我正在使用的XML看起来像:

<HistoryResponse>
  <ResponseType>resultsList</ResponseType>
  <Matches>0</Matches>
  <SessionID>75803234r23df3de</SessionID>
  <RecStart>0</RecStart>
  <ClientCode></ClientCode>
  <Results></Results>
</HistoryResponse>

I need to go through, grab the Matches and SessionID and put the values into variables.

我需要经历,抓住Matches和SessionID并将值放入变量中。

Thanks.

谢谢。

3 个解决方案

#1


1  

You can convert your xml to dictionary

您可以将xml转换为字典

var xDoc = XDocument.Parse(xmlstring); //XDocument.Load(fileName)
var dict = xDoc.Element("HistoryResponse")
               .Elements()
               .ToDictionary(e => e.Name.LocalName, e => e.Value);

and use as

并用作

string sessionID = dict["SessionID"];

#2


0  

That would definitely work with an XPath expression. Check out how to use XPath with XDocument? for info on how to do it.

这肯定适用于XPath表达式。看看如何在XDocument中使用XPath?有关如何做的信息。

#3


0  

Use LINQtoXML. (you need to import the namespace System.XML.Linq)

使用LINQtoXML。 (您需要导入名称空间System.XML.Linq)

string filePath = Server.MapPath(@"../YourFileLocation/somexml.xml");

//you can load the XML from file/stream /string etc...

XElement elem = XElement.Load(filePath);
if (elem != null)
{
   string sessionId=elem.Element("SessionID").Value;
   string matches=elem.Element("Matches").Value;
}

If you are getting the XML in a string, you can load that to your XElement using Parse method.

如果要在字符串中获取XML,可以使用Parse方法将其加载到XElement。

string xml = "<HistoryResponse><Item>XML Item A</Item></HistoryResponse>";
XElement elem = XElement.Parse(xml);

#1


1  

You can convert your xml to dictionary

您可以将xml转换为字典

var xDoc = XDocument.Parse(xmlstring); //XDocument.Load(fileName)
var dict = xDoc.Element("HistoryResponse")
               .Elements()
               .ToDictionary(e => e.Name.LocalName, e => e.Value);

and use as

并用作

string sessionID = dict["SessionID"];

#2


0  

That would definitely work with an XPath expression. Check out how to use XPath with XDocument? for info on how to do it.

这肯定适用于XPath表达式。看看如何在XDocument中使用XPath?有关如何做的信息。

#3


0  

Use LINQtoXML. (you need to import the namespace System.XML.Linq)

使用LINQtoXML。 (您需要导入名称空间System.XML.Linq)

string filePath = Server.MapPath(@"../YourFileLocation/somexml.xml");

//you can load the XML from file/stream /string etc...

XElement elem = XElement.Load(filePath);
if (elem != null)
{
   string sessionId=elem.Element("SessionID").Value;
   string matches=elem.Element("Matches").Value;
}

If you are getting the XML in a string, you can load that to your XElement using Parse method.

如果要在字符串中获取XML,可以使用Parse方法将其加载到XElement。

string xml = "<HistoryResponse><Item>XML Item A</Item></HistoryResponse>";
XElement elem = XElement.Parse(xml);