4

調査後にノードの値を取得しようとしているこのxmlがあります.APIからの応答が私に与えていた<ErrorCode>不要なものをすべて消去するため、XDocumentを使用する方が簡単であることがわかりまし\r\nた..しかし、今は方法がわかりませんXDocument を使用してその値を取得する

<PlatformResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://platform.intuit.com/api/v1">
  <ErrorMessage>OAuth Token rejected</ErrorMessage>
  <ErrorCode>270</ErrorCode>
  <ServerTime>2012-06-19T03:53:34.4558857Z</ServerTime>
</PlatformResponse>

この呼び出しを利用して値を取得できるようにしたい

 XDocument xmlResponse = XDocument.Parse(response);

XmlDocument を実行しているときに XML をクリーンアップしないため、XmlDocument を使用できません XDocument

ありがとうございました

4

3 に答える 3

10

名前空間を定義したので、次のコードを試してください。

    XDocument xmlResponse = XDocument.Load("yourfile.xml");
    //Or you can use XDocument xmlResponse = XDocument.Parse(response)
    XNamespace ns= "http://platform.intuit.com/api/v1";
    var test = xmlResponse.Descendants(ns+ "ErrorCode").FirstOrDefault().Value;

または、名前空間を使用したくない場合:

    var test3 = xmlResponse.Descendants()
                .Where(a => a.Name.LocalName == "ErrorCode")
                .FirstOrDefault().Value;
于 2012-06-19T04:17:29.193 に答える
0
XDocument doc = XDocument.Load("YouXMLPath");

var query = from d in doc.Root.Descendants()
            where d.Name.LocalName == "ErrorCode"
            select d.Value;
于 2012-06-19T04:33:40.187 に答える
0

xpath構造を使用して値を取得できます

string errorcode= xmlResponse.SelectSingleNode("PlatformResponse/ErrorCode").InnerText

またはこれ

string result = xmlResponse.Descendants("ErrorCode").Single().Value;
于 2012-06-19T04:02:57.763 に答える