1

この形式で xml 応答を取得しています

  <?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n
       <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\">\r\n  
<ErrorMessage>OAuth Token rejected</ErrorMessage>\r\n  
<ErrorCode>270</ErrorCode>\r\n  
<ServerTime>2012-06-19T00:01:31.5150146Z</ServerTime>\r\n
</PlatformResponse>

ノードの値を取得する必要があり<ErrorCode>ます。そのために、次のことを行いましたが、値を取得していません..

            XmlDocument xml = new XmlDocument();
            xml.LoadXml(xmlResponse);

            XmlNodeList xnList = xml.SelectNodes("PlatformResponse");
            foreach (XmlNode xn in xnList)
            {
                result.Message = xn["ErrorCode"].InnerText;
            }

どんな助けでも大歓迎です。

4

3 に答える 3

1

問題を引き起こしている PlatformResponse ノードに汚れがあるようです ( xmlns:xsd= など... )

このxmlの使用

String sXml = @"<?xml version='1.0' encoding='utf-8'?>
   <PlatformResponse >
        <ErrorMessage>OAuth Token rejected</ErrorMessage>
        <ErrorCode>270</ErrorCode>
        <ServerTime>2012-06-19T00:01:31.5150146Z</ServerTime>
    </PlatformResponse>";

そして、好きなものを選択します

XmlNodeList xnList = xml.SelectNodes("/PlatformResponse");

あなたのコードは正常に動作します。

于 2012-06-19T00:24:32.913 に答える
0

For this, since the attribute is on the main document element itself, you can simply do

    XmlDocument xml = new XmlDocument();
    xml.LoadXml(xmlText);
    result.Message = xml.DocumentElement["ErrorCode"].InnerText
于 2012-06-19T00:34:09.673 に答える
0

コードをテストしたところ、正常に動作します。

        XmlDocument xml = new XmlDocument();
        XmlTextReader reader = new XmlTextReader("Path_to_your_xml");
        xml.Load(reader);
        XmlNodeList xnList = xml.SelectNodes("PlatformResponse");
        foreach (XmlNode xn in xnList)
        {
            MessageBox.Show(xn["ErrorCode"].InnerText);
        }
于 2012-06-19T00:28:29.233 に答える