【问题标题】:Need help selecting xml node by attribute需要帮助按属性选择 xml 节点
【发布时间】:2019-06-29 01:32:13
【问题描述】:

我正在尝试通过属性值选择 xml 节点。

我可以通过向下钻取子节点来导航节点。我可以通过某种方式直接选择任何节点。

这是我用来测试的示例 xml 文件。

<svg version="1.1" archibusversion="24.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="1284.03 1666.86 8172 3392">
    <g id="viewer">
        <g id="mirror" transform="scale(1, 1)" stroke-width="0.05%">
            <g id="background" fill="none"/>
            <g id="annotations" fill="none" />
            <g id="text" font-family="Arial" font-size="1.3" font-weight="normal" text-style="normal" text-anchor="middle" dominant-baseline="alphabetical" fill="#000000" xml:space="preserve" />
        </g>
    </g>
</svg>

这是我的示例代码

static void Main(string[] args)
{
    XmlDocument document = new XmlDocument();
    document.Load(@"test.svg");

    XmlNode root = document.DocumentElement;

    XmlNodeList nl = document.SelectNodes("//element[@id='background']");
}

我希望能够从 svg 文件中检索节点。这个节点将有很多我需要处理的子节点。

【问题讨论】:

  • 你想从 XML 中得到什么?目前还不清楚……
  • 在您的 xpath 表达式中,element 应该是您想要的元素的实际名称,或者如果您想要任何具有该属性的元素,则省略。您的 XML 不包含任何 &lt;element&gt;
  • 最后我希望合并元素下面的元素 它将有许多子节点,它们是文件中的层。我需要重新命名图层,并将绘图元素合并到几个节点。
  • @Amy,我也尝试过使用XmlNodeList nl = document.SelectNodes("//g[@id='background']");,因为节点是 但这也会导致空列表

标签: c# xml


【解决方案1】:

这是一种使用LINQ 的方法。

public bool GetAllElementsByAttributeValue(XElement startingNode, Relationship findAmong, string attName, string attValue, out IEnumerable<XElement> matchingNodes, out string msg)
{
    bool ret = true;
    msg = "SUCCESS";
    matchingNodes = null;

    try
    {
        switch (findAmong)
        {
            case Relationship.Descendants:
                matchingNodes = from items in startingNode.Descendants()
                                where items.Attribute(attName) != null && items.Attribute(attName).Value == attValue
                                select items;
                break;

            case Relationship.Ancestors:
                matchingNodes = from items in startingNode.Ancestors()
                                where items.Attribute(attName) != null && items.Attribute(attName).Value == attValue
                                select items;
                break;

            case Relationship.Siblings:
                matchingNodes = from items in startingNode.Parent.Descendants()
                                where items.Attribute(attName) != null && items.Attribute(attName).Value == attValue
                                select items;
                break;
        }
    }
    catch (Exception ex)
    {
        msg = ex.Message;
        ret = false;
    }

    return ret;
}

编辑:

Relationship 是我定义的 enum,用于区分 Descendant、Ancestor 和 Sibling 元素。

public enum Relationship
{
    Ancestors = 0,
    Siblings = 1,
    Descendants = 2
}

用法:

static void Main()
{
    var path = @"data.xml";
    XDocument xdoc = XDocument.Load(path);
    GetAllElementsByAttributeValue(xdoc.Root, Relationship.Descendants, "id", "background", out IEnumerable<XElement> nodes, out string msg);
    Console.ReadLine();
}

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多