C#不能查询XML但可以遍历节点?
C# can't query XML but can traverse the nodes?
与有关。
我正在尝试从 XML 文件中读取节点
看起来像这样:
<?xml version="1.0" encoding="UTF-8" ?>
<AuthorIT>
<Objects>
<Media>don't care</Media>
<Style>don't care</Style>
<Book>don't care</Book>
<Topic>don't care</Topic>
<Topic>
<Object>
<Description>Performance Evidence</Description>
</Object>
<Text>This is what I want to select</Text>
</Topic>
</Objects>
</AuthorIT>
但是查询不到。我已将 XmlDocument、XPath 和 Linq 尝试到 XML.
我已在 http://XPather.com 上验证我的 XPath 是正确的。
/* this doesn't work but the XPath has been verified */
XPathNavigator nav;
XPathDocument docNav;
string xPath;
docNav = new XPathDocument(localFile);
nav = docNav.CreateNavigator();
xPath = "//Topic[Object/Description = 'Performance Evidence']/Text";
string value = nav.SelectSingleNode(xPath).Value;
我可以使用 XmlDocument 遍历 XML 个节点,但我不想用一系列嵌套的 foreach 循环来代替单个查询。
XmlDocument doc = new XmlDocument();
doc.Load(localFile);
XmlNodeList xmlNodes = doc.SelectNodes("/");
/* direct query doesn't work, but traversing the nodes does? */
foreach (XmlNode node in xmlNodes)
{
if (node.Name == "#document")
{
foreach (XmlNode subNode in node.ChildNodes)
{
if (subNode.Name == "AuthorIT")
{
foreach (XmlNode subSubNode in subNode.ChildNodes)
{
if (subSubNode.Name == "Objects")
{
foreach (XmlNode subSubSubNode in subSubNode.ChildNodes)
{
if (subSubSubNode.Name == "Topic")
{
// I didn't finish writing this, because it's a ridiculous way to do it... but it works
}
}
}
}
}
}
}
}
我做错了什么吗?
一些属性的XML文件会导致这种情况? (如果是这样,我该如何解决?)
如果您只需要存储在 <Text>
属性中的值,则无需遍历所有内容,请尝试以下操作:
var doc = XDocument.Parse(File.ReadAllText(filePath));
var value = doc.XPathSelectElement("/AuthorIT/Objects/Topic/Text").Value;
示例中遗漏的内容 XML 是解决方案的关键...命名空间!
我找到了另一个问题的答案:Using Xpath With Default Namespace in C#
与
我正在尝试从 XML 文件中读取节点 看起来像这样:
<?xml version="1.0" encoding="UTF-8" ?>
<AuthorIT>
<Objects>
<Media>don't care</Media>
<Style>don't care</Style>
<Book>don't care</Book>
<Topic>don't care</Topic>
<Topic>
<Object>
<Description>Performance Evidence</Description>
</Object>
<Text>This is what I want to select</Text>
</Topic>
</Objects>
</AuthorIT>
但是查询不到。我已将 XmlDocument、XPath 和 Linq 尝试到 XML.
我已在 http://XPather.com 上验证我的 XPath 是正确的。
/* this doesn't work but the XPath has been verified */
XPathNavigator nav;
XPathDocument docNav;
string xPath;
docNav = new XPathDocument(localFile);
nav = docNav.CreateNavigator();
xPath = "//Topic[Object/Description = 'Performance Evidence']/Text";
string value = nav.SelectSingleNode(xPath).Value;
我可以使用 XmlDocument 遍历 XML 个节点,但我不想用一系列嵌套的 foreach 循环来代替单个查询。
XmlDocument doc = new XmlDocument();
doc.Load(localFile);
XmlNodeList xmlNodes = doc.SelectNodes("/");
/* direct query doesn't work, but traversing the nodes does? */
foreach (XmlNode node in xmlNodes)
{
if (node.Name == "#document")
{
foreach (XmlNode subNode in node.ChildNodes)
{
if (subNode.Name == "AuthorIT")
{
foreach (XmlNode subSubNode in subNode.ChildNodes)
{
if (subSubNode.Name == "Objects")
{
foreach (XmlNode subSubSubNode in subSubNode.ChildNodes)
{
if (subSubSubNode.Name == "Topic")
{
// I didn't finish writing this, because it's a ridiculous way to do it... but it works
}
}
}
}
}
}
}
}
我做错了什么吗?
一些属性的XML文件会导致这种情况? (如果是这样,我该如何解决?)
如果您只需要存储在 <Text>
属性中的值,则无需遍历所有内容,请尝试以下操作:
var doc = XDocument.Parse(File.ReadAllText(filePath));
var value = doc.XPathSelectElement("/AuthorIT/Objects/Topic/Text").Value;
示例中遗漏的内容 XML 是解决方案的关键...命名空间!
我找到了另一个问题的答案:Using Xpath With Default Namespace in C#