[英]Unable to get values from xml nodes
I am trying to parse through xml and get some values and have tried multiple approaches but i am unable to get the values.我正在尝试解析 xml 并获取一些值并尝试了多种方法,但我无法获取这些值。
this is what i have tried so far这是我到目前为止尝试过的
string fileName = @"C:\Users\one\OneDrive\Desktop\PP\SnippetWithImage.idms";
string xmlString = System.IO.File.ReadAllText(fileName);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlNodeList spreadNodes = xmlDoc.GetElementsByTagName("spread");
foreach (XmlNode spreadNode in spreadNodes)
{
var linkNodes = spreadNode.SelectNodes("//rectangle/image/link");
foreach (XmlNode linkNode in linkNodes)
{
Console.WriteLine("path : " + linkNode.SelectSingleNode("LinkResourceURI").InnerText);
}
}
Here spreadNodes count is always 0.这里的 spreadNodes 计数始终为 0。
I have tried this with XElements as well and used Decendents but sill the same result.我也尝试过使用 XElements 并使用 Decendents,但结果相同。
Here is the xml structure https://codebeautify.org/xmlviewer/y22db1f26这里是xml结构https://codebeautify.org/xmlviewer/y22db1f26
the file which i am loading is an InDesign Snippet file.我正在加载的文件是一个 InDesign 片段文件。 I am trying to get the linkResourceURI property value from links which is a child of image which is a child of rectangle which is a child of spread.
我正在尝试从链接中获取 linkResourceURI 属性值,链接是图像的子项,矩形是传播的子项。
Some quick debugging shows that spreadNodes
is empty, to start with.一些快速调试显示
spreadNodes
是空的,首先。 The most obvious cause is that the element name is Spread
not spread
, and if we make this change, we see spreadNodes
has one element .最明显的原因是元素名称是
Spread
而不是spread
,如果我们进行此更改,我们会看到spreadNodes
有一个元素。
Next, it's obvious that spreadNode.SelectNodes("//rectangle/image/link")
isn't returning anything.接下来,
spreadNode.SelectNodes("//rectangle/image/link")
显然没有返回任何内容。 Again, that's probably capitalisation: change it to //Rectangle/Image/Link
, and we get a NullReferenceException
.同样,这可能是大写:将其更改为
//Rectangle/Image/Link
,我们得到一个NullReferenceException
。 Progress, at least.进步,至少。
We're getting that exception because linkNode.SelectSingleNode("LinkResourceURI")
is returning null. It turns out that's because LinkResourceURI
is an attribute, and you're looking for an element.我们收到该异常是因为
linkNode.SelectSingleNode("LinkResourceURI")
返回 null。原来这是因为LinkResourceURI
是一个属性,而您正在寻找一个元素。 Change that to linkNode.Attributes["LinkResourceURI"].InnerText
, and everything works as expected .将其更改为
linkNode.Attributes["LinkResourceURI"].InnerText
,一切都按预期工作。
XmlNodeList spreadNodes = xmlDoc.GetElementsByTagName("Spread");
foreach (XmlNode spreadNode in spreadNodes)
{
var linkNodes = spreadNode.SelectNodes("//Rectangle/Image/Link");
foreach (XmlNode linkNode in linkNodes)
{
Console.WriteLine("path : " + linkNode.Attributes["LinkResourceURI"].InnerText);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.