导航xml节点;

时间:2015-10-20 14:25:06

标签: c#

嗨我有另一个服务返回的xml数据。看起来像这样

var catData = [
 {"LkpMasterID":491,"LkpMasterCode":"CAT  INSURANCE","LkpMasterDescription":"25","Attribute":"Cat Values","Dependency":null,"LkpName":"CAT INSURANCE","IsAttribute":false,"IsActive":true,"CreatedBy":52834,"CreatedOn":"2015-09-15T15:25:46.273","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":1},

 {"LkpMasterID":492,"LkpMasterCode":"CAT SUPPLEMENTS","LkpMasterDescription":"50","Attribute":"Cat Values","Dependency":null,"LkpName":"CAT SUPPLEMENTS","IsAttribute":false,"IsActive":true,"CreatedBy":52834,"CreatedOn":"2015-09-15T15:28:36.2","UpdatedBy":56366,"UpdatedOn":"2015-09-16T10:26:36.95","IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":2},

 {"LkpMasterID":493,"LkpMasterCode":"OTHER CATS","LkpMasterDescription":"30","Attribute":"Cat Values","Dependency":null,"LkpName":"OTHER CATS","IsAttribute":false,"IsActive":true,"CreatedBy":56366,"CreatedOn":"2015-09-16T10:27:37.777","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":3},

 {"LkpMasterID":495,"LkpMasterCode":"SHORT-TERM CATS","LkpMasterDescription":"30","Attribute":"Cat Values","Dependency":null,"LkpName":"SHORT-TERM CATS","IsAttribute":false,"IsActive":true,"CreatedBy":56366,"CreatedOn":"2015-10-02T00:00:00","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":4}
  ]

var arr = $.map(catData, function(x) { return x; })

console.log(arr)

我需要Data,Message和ReturnCode的值。数据(PRINT'这是一个测试#2')节点内的值可以是单行或数千行..

我正在使用此C#代码获取值

XmlDocument xm = new XmlDocument();

<?xml version="1.0" encoding="UTF-8"?>
<response xmlns="http://test.com/group/application" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Response>
<Response>
<ReturnCode>0</ReturnCode>
<Message>Sucess</Message>
<Data>PRINT 'This is a test #2'</Data>
</Response>
</Response>
</response>
        string Response = obj.getContent(str, 1, 73810, SHA);



        //Console.WriteLine("Response" + Response);
        xm.LoadXml(Response);



        Console.WriteLine(xm.InnerXml);

        XmlNode oldCd;
        XmlElement root = xm.DocumentElement;
        Console.WriteLine(root.InnerText);
        oldCd = root.SelectSingleNode("/response/Response/Response/ReturnCode/Message/Data/");

1 个答案:

答案 0 :(得分:0)

试试这个

XmlDocument xml = new XmlDocument();
xml.LoadXml(myXmlString); //myXmlString is the xml file in string //copying xml to string: string myXmlString = xmldoc.OuterXml.ToString();
XmlNodeList xnList = xml.SelectNodes("/responset[@*]/Response");
foreach (XmlNode xn in xnList)
{
XmlNode response = xn.SelectSingleNode("Response");
if (response != null)
{
string rc = response["ReturnCode"].InnerText;
string msg = example["Message"].InnerText;
string data = example["Data"].InnerText;
}
}