1

以下は、XmlDocumentに読み込まれたXMLファイルです。

<Test xmlns="http://api.test.com/v2" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Result id="2015" description="Invalid Token" />
</Test >

必要なのは、TextBoxに格納されている「id」属性値(「2015」)です。

これがXmlDocumentのロード方法です

XmlDocument updateUser = new XmlDocument();
updateUser.Load(response.GetResponseStream());

ここまでうまくいきます。

次に、名前空間を作成してノードを検索します

XmlNamespaceManager nsmgr = new XmlNamespaceManager(updateUser.NameTable);
nsmgr.AddNamespace("restup", "http://api.test.com/v2");

XmlNodeList locationElements1 = updateUser.SelectNodes("//restup:Test", nsmgr);
foreach (XmlNode Test in locationElements1)
{
//What DO I do here to get the value of 'id' attribute from the 'Result' node and save it in txtTest Textbox.

}
4

3 に答える 3

3
var id = Test.FirstChild.Attributes["id"].Value;
于 2012-08-14T15:47:25.237 に答える
0
string idString = Test.FirstChild.Attributes["id"].ToString();
于 2012-08-14T15:51:59.837 に答える
0

こんにちはこれは便利な別の方法です

XmlTextReader reader = new XmlTextReader(fileLocation); //fileLocation is the Path of the XML file
while (reader.Read())
{

  if (reader.NodeType == XmlNodeType.Element) //if the node is an element (not a comment, CDATA, or text)
     if (reader.Name == "Result")
       textBox1.Text = reader.GetAttribute("id");

}
reader.Close();
于 2012-08-16T11:54:31.413 に答える