C# XML --> 如何获取属于该attributes元素的属性值和xpath

标签 c# xml xpath xml-parsing

我正在寻找一种遍历 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>

最佳答案

恕我直言,LINQ to XML 更简单:

var document = XDocument.Load(fileName);

var captions = document.Descendants()
    .Select(arg => arg.Attribute("Caption"))
    .Where(arg => arg != null)
    .Select(arg => arg.Value)
    .ToList();

[更新]

为每个具有 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;
}

关于C# XML --> 如何获取属于该attributes元素的属性值和xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6160879/

相关文章:

c# - 如何在通用 Windows 应用程序中垂直对齐 StackPanel?

c# - 日期时间方程不返回任何值。服务器 2008

java - 当我点击 <s :url> with params I have a Unexpected exception in class ActionSupport in Struts2 时

python - 提取标签之间的值 - python xpath

java - 双击操作不起作用,而单击操作对 Selenium 中的元素起作用

c# - 按钮单击事件未在 ASP .Net 中的使用控件内触发

c# - 将嵌入 Razor 的 JavaScript 代码提取到单独文件中的最佳实践

java - 如何在 Android 中显示本地 html 文件?

xml - Chrome 显示 : Resource interpreted as Stylesheet but transferred with MIME type application/xml

XPATH : replace every ohter whitespace