[英]Why doesn't my XmlReader.GetAttribute() return a value?
我正在嘗試用C#解析我的XML。
這是相關文件的一部分:
<holder name="wnd_login" width="300" x="20" height="180">...</holder>
這是應該讀取它的代碼:
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "holder":
Holder holder = new Holder(reader.GetAttribute("name"));
...
}
}
}
我讀到了常見的錯誤是忘記檢查元素是否是一個開始元素。 我添加了它,但GetAttribute仍然返回null。 任何想法?
也許你需要首先使用XPath表示法獲取XmlNodes,然后像這樣迭代遍歷XmlNodes:
foreach(XmlNode node in XmlNodes){ if (node["holder"].HasAttribues != null && node["holder"].Attributes.Count >1){ for (int i = 0; i < node["holder"].Attributes.Count; i++){ try{ XmlAttribute attr = node["holder"].Attributes[i]; if (attr != null){ .... } }catch(XmlException xmlEx){ // Do something here with this...output to log? } } } }
希望這會有所幫助,最好的問候,湯姆。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.