2015-10-20 82 views
-5

嗨我有一个xml数据从另一个服务返回。它看起来像这样导航xml节点;

<?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> 

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

我使用此C#代码以获得值

XmlDocument的XM =新的XmlDocument ();

 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/"); 

static void Main() 
    { 
     try 
     { 

      svc obj = new svc(); 
      .. 
      //XmlDocument xm = new XmlDocument(); 

      string rsp = obj.getContent(..; 


      String myEncodedString; 
      myEncodedString = obj.XmlDecode(rsp); 

      XNamespace xmlns = XNamespace.Get("http://xxxx.com/xxx/xx"); 

      XDocument doc = XDocument.Parse(myEncodedString); 

      Console.WriteLine(obj.Return_Message_Data("ReturnCode", myEncodedString)); 

      Console.WriteLine(obj.Return_Message_Data("Message", myEncodedString)); 

      Console.WriteLine(obj.Return_Message_Data("Data", myEncodedString));    

      Console.ReadLine(); 
     } 

     catch (Exception e) 
     { 
      Console.WriteLine(e); 
      Console.ReadLine(); 
     } 

    } 
+4

你最好开始再编码! –

+0

是的,我从c#应用程序访问这个。我是 XmlDocument xm = new XmlDocument(); 串RSP = obj.getContent(STR,1,73810,SHA); xm.LoadXml(Rsp); Console.WriteLine(xm.InnerXml); XmlNode oldCd; XmlElement root = xm.DocumentElement; Console.WriteLine(root.InnerText); oldCd = root.SelectSingleNode(“/ response/Response/Response/ReturnCode/Message/Data /”); 但我在Data列中没有看到任何值。 谢谢 – jramacha

+3

您需要提出具体问题。这听起来更像是你想要一个完整的解决方案。 – leigero

回答

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; 
} 
} 
+0

这没有奏效 – jramacha