C# XML -->如何获取属于该属性元素的属性值和xpath
我正在寻找一种方法来遍历 XML 文件并获取文本和 xpath 的一些属性。 但我不知道该怎么做。我知道如何从我想要的属性中获取所有文本,但问题是我看不到它所在的 xpath。 有人可以帮助我吗? 代码 =
// XML settings
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
// Loop through the XML to get all text from the right attributes
using (XmlReader reader = XmlReader.Create(sourceFilepathTb.Text, settings))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.HasAttributes)
{
if (reader.GetAttribute("Caption") != null)
{
MessageBox.Show(reader.GetAttribute("Caption"));
}
}
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<Test Description="Test XML" VersionFormat="123" ProtectedContentText="(Test test)">
<Testapp>
<TestappA>
<A Id="0" Caption="Test 0" />
<A Id="1" Caption="Test 1" />
<A Id="2" Caption="Test 2" />
<A Id="3" Caption="Test 3">
<AA>
<B Id="4" Caption="Test 4" />
</AA>
</A>
</TestappA>
<AA>
<Reason Id="5" Caption="Test 5" />
<Reason Id="6" Caption="Test 6" />
<Reason Id="7" Caption="Test 7" />
</AA>
</Testapp>
</Test>
I'm looking for a way to walk through a XML file and get for a few attributes the text and the xpath.
But I have no idea how to do that. I know how to get all the text from the attributes I want, but the problem with that is that I cann't see the xpath where it is in.
Can some one help me?
The code =
// XML settings
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
// Loop through the XML to get all text from the right attributes
using (XmlReader reader = XmlReader.Create(sourceFilepathTb.Text, settings))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.HasAttributes)
{
if (reader.GetAttribute("Caption") != null)
{
MessageBox.Show(reader.GetAttribute("Caption"));
}
}
}
}
}
The XML:
<?xml version="1.0" encoding="utf-8"?>
<Test Description="Test XML" VersionFormat="123" ProtectedContentText="(Test test)">
<Testapp>
<TestappA>
<A Id="0" Caption="Test 0" />
<A Id="1" Caption="Test 1" />
<A Id="2" Caption="Test 2" />
<A Id="3" Caption="Test 3">
<AA>
<B Id="4" Caption="Test 4" />
</AA>
</A>
</TestappA>
<AA>
<Reason Id="5" Caption="Test 5" />
<Reason Id="6" Caption="Test 6" />
<Reason Id="7" Caption="Test 7" />
</AA>
</Testapp>
</Test>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恕我直言,LINQ to XML 更简单:
[更新]
查找具有 Caption 属性的每个元素的 XPath:
IMHO, LINQ to XML is simpler:
[Update]
To find XPath for each element that has Caption attribute:
这是一个开始。您可以练习如何在前面添加斜杠。
顺便说一句,我尽量避免将代码嵌套得那么深。太难读了。
干杯。基思.
编辑:(几天后)
我终于有了一些属于自己的时间......所以我坐下来“正确”地做了这件事。事实证明这比我最初想象的要困难得多。恕我直言,这种递归解决方案仍然比 XSLT 更容易理解,我发现 XSLT 非常令人困惑;-)
Here's a start. You can workout how to prepend the leading slash.
And just BTW, I try to avoid nesting code that deeply. Too hard to read.
Cheers. Keith.
EDIT: (days later)
I finally got some time to myself... So I sat down and did this "correctly". It turned out to be a lot harder than I first thought. IMHO, this recursive solution is still easier to groc than XSLT, which I find infinetely confusing ;-)
简单而精确的 XSLT 解决方案:
当此转换应用于提供的 XML 文档时:
生成所需的正确结果:
请注意:这是迄今为止提出的唯一解决方案,可为任何单个
Caption
属性生成精确的 XPath 表达式。/Test/Testapp/TestappA/A/@Caption
选择4个属性节点,而:
/Test/Testapp/TestappA/A[2]/@Caption< /code>
仅选择一个属性节点,这才是真正想要的。
A simple and precise XSLT solution:
when this transformation is applied on the provided XML document:
the wanted, correct result is produced:
Do note: This is the only solution presented so far, that generates the exact XPath expression for any single
Caption
attribute./Test/Testapp/TestappA/A/@Caption
selects 4 attribute nodes, whereas:
/Test/Testapp/TestappA/A[2]/@Caption
selects just a single attribute node ans is what really is wanted.