简体   繁体   English

C#xml阅读器,元素名称相同

[英]C# xml reader, same element name

I'm trying to read an element from my xml file. 我正在尝试从我的xml文件中读取一个元素。 I need to read an string in an "link" element inside the "metadata", but there are 2 elements called "link", I only need the second one: 我需要在“元数据”中的“link”元素中读取一个字符串,但有2个元素称为“链接”,我只需要第二个:

<metadata>
<name>visit-2015-02-18.gpx</name>
<desc>February 18, 2015. Corn</desc>
<author>
    <name>text</name>
    <link href="http://snow.traceup.com/me?id=397760"/>
</author>
<link href="http://snow.traceup.com/stats/u?uId=397760&amp;vId=1196854"/>
<keywords>Trace, text</keywords>

I need to read this line: 我需要阅读这一行:

<link href="http://snow.traceup.com/stats/u?uId=397760&amp;vId=1196854"/>

This is the working code for the first "link" tag, it works fine, 这是第一个“链接”标签的工作代码,它工作正常,

        public string GetID(string path)
    {
        string id = "";
        XmlReader reader = XmlReader.Create(path);
        while (reader.Read())
        {
            if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "link"))
            {
                    if (reader.HasAttributes)
                    {
                        id = reader.GetAttribute("href");
                        MessageBox.Show(id + "= first id");
                        return id;
                        //id = reader.ReadElementContentAsString();
                    }
                }
            }
            return id;
        }

Does anyone know how I can skip the first "link" element? 有谁知道如何跳过第一个“链接”元素? or check if reader.ReadElementContentAsString() contains "Vid" or something like that? 或检查reader.ReadElementContentAsString()是否包含“Vid”或类似的内容?

I hope you can help me. 我希望你能帮助我。

xpath is the answer :) xpath就是答案:)

        XmlReader reader = XmlReader.Create(path);
        XmlDocument doc = new XmlDocument();
        doc.Load(reader);
        XmlNodeList nodes = doc.SelectNodes("metadata/link");
        foreach(XmlNode node in nodes)
             Console.WriteLine(node.Attributes["href"].Value);

Use the String.Contains method to check if the string contains the desired substring, in this case vId : 使用String.Contains方法检查字符串是否包含所需的子字符串,在本例中为vId

public string GetID(string path)
{
    XmlReader reader = XmlReader.Create(path);
    while (reader.Read())
    {
        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "link"))
        {
        if (reader.HasAttributes)
        {
            var id = reader.GetAttribute("href");
            if (id.Contains(@"&vId"))
            {
                MessageBox.Show(id + "= correct id");
                return id;
            }
        }
    }
    return String.Empty;
}

If acceptable you can also use LINQ2XML : 如果可以接受,您也可以使用LINQ2XML

var reader = XDocument.Load(path); // or XDocument.Parse(path);
// take the outer link
Console.WriteLine(reader.Root.Element("link").Attribute("href").Value);

The output is always: 输出总是:

http://snow.traceup.com/stats/u?uId=397760&vId=1196854= first id

Another options is to use XPath like @user5507337 suggested. 另一个选择是使用像@ user5507337建议的XPath。

XDocument example: XDocument示例:

var xml = XDocument.Load(path); //assuming path points to file
var nodes = xml.Root.Elements("link");
foreach(var node in nodes)
{
    var href = node.Attribute("href").Value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM