string str = "<samples><sample>cancel</sample><sample intentref='BUSINESS_PAY'>make payment</sample></samples>";
XmlDocument xml = new XmlDocument();
xml.LoadXml(str);
XmlNodeList xnList = xml.SelectNodes("/samples/sample");
foreach (XmlNode xn in xnList)
{
string intent = xn.Attributes["intentref"].Value;
Console.WriteLine(intent);
}
如果您查看 (str) 變數,您可以看到 XML 標記。如您所見,有些元素沒有“intentref”屬性,有些則有。我想要做的是這樣列印:
| 意圖 | 樣本 |
|---|---|
| 未分配 | 取消 |
| BUSINESS_PAY | 付款 |
謝謝,
uj5u.com熱心網友回復:
string intent = xn.Attributes["intentref"]?.Value ?? "Unassigned";
在這里看到它的作業:
https://dotnetfiddle.net/ELgyJ6
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/492127.html
上一篇:2個XML檔案,1個XLST:使用共享ID號提取資料
下一篇:滿足條件時的XSL變數標記名稱
