[英]C# - Unable to get node value from xml file
加載 XML 文件后,我試圖從節點獲取內部文本。 我已經用另一個文件嘗試過這段代碼,它奏效了。 但在這里我無法獲得節點值。 誰能指出我做錯了什么?
XML 文件 - restaurant_reviews.xml
<?xml version="1.0"?> <restaurants xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.algonquincollege.com/onlineservice/reviews"> <restaurant> <name>Laughing Man</name> <logo> <name>Visit the Laughing Man</name> <imagefile>laughing-man.gif</imagefile> <width unit="px">50</width> <height unit="px">200</height> </logo> </restaurant> <restaurant> <name>Gong’s Asian Cuisine</name> <logo> <name/> <imagefile>gong-asian-cuisine.gif</imagefile> <width unit="px">150</width> <height unit="px">250</height> </logo> </restaurant> </restaurants>
C# 代碼
List<string> names = new List<string>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(HttpContext.Current.Server.MapPath(@"~/App_Data/restaurant_reviews.xml"));
XmlNodeList nodes = xmlDoc.SelectNodes("/restaurants/restaurant");
foreach (XmlNode itemNode in nodes)
{
XmlNode titleNode = itemNode.SelectSingleNode("name");
if (titleNode != null)
{
names.Add(titleNode.InnerText);
}
}
雖然這個問題已經有一個公認的答案,但我還是想添加這個,因為以這種方式刪除命名空間和操作 XML 對我來說並不合適,我懷疑它是出於某種原因添加的。
我認為正確的方法是將 XML 命名空間管理器添加到您的 XPath 查詢中。
var nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace("r", "http://www.algonquincollege.com/onlineservice/reviews");
然后在 SelectNodes 和 SelectSingleNodes 中,將命名空間添加到查詢並傳入管理器,就像這樣。
XmlNodeList nodes = xmlDoc.SelectNodes("/r:restaurants/r:restaurant", nsMgr);
和
XmlNode titleNode = itemNode.SelectSingleNode("r:name", nsMgr);
但是,如果您對其他解決方案感到滿意並且可以以這種方式操縱它,那么我想就去做吧。
如果您在您的 xml 中刪除此xmlns="http://www.algonquincollege.com/onlineservice/reviews"
它將起作用。 我不知道為什么,但是xmlDoc.SelectNodes("/restaurants/restaurant");
找不到任何具有此 xmlns 命名空間的節點。
這是我使用的代碼,它有效:
string xml = @"<?xml version=""1.0""?>
<restaurants xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<restaurant>
<name>Laughing Man</name>
<logo>
<name>Visit the Laughing Man</name>
<imagefile>laughing-man.gif</imagefile>
<width unit=""px"">50</width>
<height unit=""px"">200</height>
</logo>
</restaurant>
<restaurant>
<name>Gong’s Asian Cuisine</name>
<logo>
<name/>
<imagefile>gong-asian-cuisine.gif</imagefile>
<width unit=""px"">150</width>
<height unit=""px"">250</height>
</logo>
</restaurant>
</restaurants>";
List<string> names = new List<string>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
XmlNodeList nodes = xmlDoc.SelectNodes("/restaurants/restaurant");
foreach (XmlNode itemNode in nodes)
{
XmlNode titleNode = itemNode.SelectSingleNode("name");
if (titleNode != null)
{
names.Add(titleNode.InnerText);
}
}
問題出在命名空間上。 感謝評論部分中的@Sean 我已經解決了這個問題。 感謝@Presi 還指出了命名空間屬性是問題所在。
List<string> names = new List<string>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(HttpContext.Current.Server.MapPath(@"~/App_Data/restaurant_reviews.xml"));
var namespaceName = "ns";
var namespacePrefix = string.Empty;
XmlNamespaceManager nameSpaceManager = null;
if (xmlDoc.LastChild.Attributes != null)
{
var xmlns = xmlDoc.LastChild.Attributes["xmlns"];
if (xmlns != null)
{
nameSpaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
nameSpaceManager.AddNamespace(namespaceName, xmlns.Value);
namespacePrefix = namespaceName + ":";
}
}
XmlNodeList nodes = xmlDoc.SelectNodes(string.Format("/{0}restaurants/{0}restaurant", namespacePrefix), nameSpaceManager);
foreach (XmlNode itemNode in nodes)
{
XmlNode titleNode = itemNode.SelectSingleNode(namespacePrefix + "name", nameSpaceManager);
if (titleNode != null)
{
names.Add(titleNode.InnerText);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.