[英]C# XML --> How to get the attribute value and xpath that belongs to this attributes element
I'm looking for a way to walk through a XML file and get for a few attributes the text and the xpath.我正在寻找一种方法来遍历 XML 文件并获取文本和 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?
我知道如何从我想要的属性中获取所有文本,但问题是我看不到它所在的 xpath。有人可以帮我吗? 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:
<?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>
IMHO, LINQ to XML is simpler:恕我直言,LINQ 到 XML 更简单:
var document = XDocument.Load(fileName);
var captions = document.Descendants()
.Select(arg => arg.Attribute("Caption"))
.Where(arg => arg != null)
.Select(arg => arg.Value)
.ToList();
[Update] [更新]
To find XPath for each element that has Caption attribute:要为每个具有 Caption 属性的元素查找 XPath:
var captions = document.Descendants()
.Select(arg =>
new
{
CaptionAttribute = arg.Attribute("Caption"),
XPath = GetXPath(arg)
})
.Where(arg => arg.CaptionAttribute != null)
.Select(arg => new { Caption = arg.CaptionAttribute.Value, arg.XPath })
.ToList();
private static string GetXPath(XElement el)
{
if (el.Parent == null)
return "/" + el.Name.LocalName;
var name = GetXPath(el.Parent) + "/" + el.Name.LocalName;
if (el.Parent.Elements(el.Name).Count() != 1)
return string.Format(@"{0}[{1}]", name, (el.ElementsBeforeSelf(el.Name).Count() + 1));
return name;
}
Here's a start.这是一个开始。 You can workout how to prepend the leading slash.
您可以锻炼如何在前导斜杠之前添加。
using System;
using System.Xml;
namespace ConsoleApplication4 {
class Program {
static void Main(string[] args) {
// 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("Test.xml", settings) ) {
while ( reader.Read() ) {
if ( reader.NodeType == XmlNodeType.Element ) {
Console.Write(reader.LocalName + "/"); // <<<<----
if ( reader.HasAttributes ) {
if ( reader.GetAttribute("Caption") != null ) {
Console.WriteLine(reader.GetAttribute("Caption"));
}
}
}
}
}
Console.Write("Press any key ..."); Console.ReadKey();
}
}
}
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 更容易 groc,我觉得这非常令人困惑;-)
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
namespace ConsoleApplication4
{
public class XPathGrepper : IDisposable
{
private XmlReader _rdr;
private TextWriter _out;
public XPathGrepper(string xmlFilepath, TextWriter output) {
_rdr = CreateXmlReader(xmlFilepath);
_out = output;
}
private static XmlReader CreateXmlReader(string xmlFilepath) {
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
return XmlReader.Create(xmlFilepath, settings);
}
// descends through the XML, printing the xpath to each @attributeName.
public void Attributes(string attributeName) {
Attributes(_rdr, attributeName, "/");
}
// a recursive XML-tree descent, printing the xpath to each @attributeName.
private void Attributes(XmlReader rdr, string attrName, string path) {
// skip the containing element of the subtree (except root)
if ( "/" != path )
rdr.Read();
// count how many times we've seen each distinct path.
var kids = new Histogram();
// foreach node at-this-level in the tree
while ( rdr.Read() ) {
if (rdr.NodeType == XmlNodeType.Element) {
// build the xpath-string to this element
string nodePath = path + _rdr.LocalName;
nodePath += "[" + kids.Increment(nodePath) + "]/";
// print the xpath to the Caption attribute of this node
if ( _rdr.HasAttributes && _rdr.GetAttribute(attrName) != null ) {
_out.WriteLine(nodePath + "@" + attrName);
}
// recursively read the subtree of this element.
Attributes(rdr.ReadSubtree(), attrName, nodePath);
}
}
}
public void Dispose() {
if ( _rdr != null ) _rdr.Close();
}
private static void Pause() {
Console.Write("Press enter to continue....");
Console.ReadLine();
}
static void Main(string[] args) {
using ( var grep = new XPathGrepper("Test.xml", Console.Out) ) {
grep.Attributes("Caption");
}
Pause();
}
private class Histogram : Dictionary<string, int>
{
public int Increment(string key) {
if ( base.ContainsKey(key) )
base[key] += 1;
else
base.Add(key, 1);
return base[key];
}
}
}
}
A simple and precise XSLT solution :一个简单而精确的 XSLT 解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="//@Caption"/>
</xsl:template>
<xsl:template match="@Caption">
<xsl:apply-templates select="." mode="path"/>
<xsl:value-of select="concat(': ',.,'
')"/>
</xsl:template>
<xsl:template match="@Caption" mode="path">
<xsl:for-each select="ancestor::*">
<xsl:value-of select="concat('/',name())"/>
<xsl:variable name="vSiblings" select=
"count(../*[name()=name(current())])"/>
<xsl:if test="$vSiblings > 1">
<xsl:value-of select="
concat('[',
count(preceding-sibling::*
[name()=name(current())]) +1,
']'
)"/>
</xsl:if>
</xsl:for-each>
<xsl:text>/@Caption</xsl:text>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document :当此转换应用于提供的 XML 文档时:
<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>
the wanted, correct result is produced :产生了想要的正确结果:
/Test/Testapp/TestappA/A[1]/@Caption: Test 0
/Test/Testapp/TestappA/A[2]/@Caption: Test 1
/Test/Testapp/TestappA/A[3]/@Caption: Test 2
/Test/Testapp/TestappA/A[4]/@Caption: Test 3
/Test/Testapp/TestappA/A[4]/AA/B/@Caption: Test 4
/Test/Testapp/AA/Reason[1]/@Caption: Test 5
/Test/Testapp/AA/Reason[2]/@Caption: Test 6
/Test/Testapp/AA/Reason[3]/@Caption: Test 7
Do note : This is the only solution presented so far, that generates the exact XPath expression for any single Caption
attribute.请注意:这是迄今为止提出的唯一解决方案,可为任何单个
Caption
属性生成精确的 XPath 表达式。
/Test/Testapp/TestappA/A/@Caption
selects 4 attribute nodes, whereas :选择 4 个属性节点,而:
/Test/Testapp/TestappA/A[2]/@Caption
selects just a single attribute node ans is what really is wanted .只选择一个属性节点 ans 是真正想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.