C# 将GetElementsByTagName与多个标记一起使用

C# 将GetElementsByTagName与多个标记一起使用,c#,xml,xmldom,C#,Xml,Xmldom,我的XML中有许多标记 <?xml version="1.0" encoding="UTF-8"?> <xmi:XMI xmlns:util="http://" xmlns:tcas="http://" xmlns:xmi="http://www.omg.org/XMI" xmlns:cas="http:/" xmlns:type10="http:///" xmlns:ne="http:///" xmlns:group3="http:///org/" xmi:version=

我的XML中有许多标记

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:util="http://" xmlns:tcas="http://" xmlns:xmi="http://www.omg.org/XMI" xmlns:cas="http:/" xmlns:type10="http:///" xmlns:ne="http:///" xmlns:group3="http:///org/"  xmi:version="2.0">
    <cas:NULL xmi:id="0"/>
    <group1:Black xmi:id="19" code="1" ref="0" mask="23086" id="SIMPLE_Black"/>
    <group1:White xmi:id="25" code="2" ref="0" mask="21" WhiteNumber="0"/>
    <group1:White xmi:id="31" code="7" ref="23" mask="39" WhiteNumber="1"/>
    <group1:White xmi:id="37" code="3" ref="53" mask="68" WhiteNumber="2"/>
    <group1:White xmi:id="43" code="7" ref="71" mask="86" WhiteNumber="3"/>
    <group1:White xmi:id="49" code="3" ref="88" mask="102" WhiteNumber="4"/>
    <group2:Pink xmi:id="8745" code="4" ref="20613" mask="20614" other="5904"/>
    <group2:Pink xmi:id="8753" code="7" ref="20624" mask="20625" other="5907"/>
    <group2:Pink xmi:id="8761" code="5" ref="20625" mask="20626" other="5908"/>
    <group2:Pink xmi:id="8769" code="2" ref="20640" mask="20641" other="5911"/>
    <group2:Pink xmi:id="8777" code="1" ref="20641" mask="20642" other="5912"/>
    <group2:Pink xmi:id="8785" code="6" ref="20701" mask="20702" other="5923"/>
    <group2:Blue xmi:id="31715" code="3" ref="6959" mask="6966" other="2457" />
    <group2:Blue xmi:id="31727" code="5" ref="6967" mask="6971" other="2458" />
    <group2:Blue xmi:id="31747" code="7" ref="6973" mask="6977" other="2460" />
    <group2:Blue xmi:id="31759" code="2" ref="6978" mask="6981" other="2461" />
    <group2:Blue xmi:id="31771" code="8" ref="6982" mask="6991" other="2463" />
    <group2:Blue xmi:id="31783" code="8" ref="6992" mask="6993" other="2464" />
    <group2:Blue xmi:id="31795" code="8" ref="6994" mask="7002" other="2465" />
    <group2:Blue xmi:id="31807" code="9" ref="7003" mask="7013" other="2466" />
    <group2:Blue xmi:id="31827" code="3" ref="7015" mask="7022" other="2468" />
    <group2:Blue xmi:id="31847" code="1" ref="7024" mask="7026" other="2470" />
    <group2:Red xmi:id="29184" code="2" ref="6100" mask="6101" other="2178" />
    <group2:Red xmi:id="29217" code="1" ref="6105" mask="6106" other="2182" />
    <group2:Red xmi:id="29234" code="4" ref="6109" mask="6110" other="2184" />
    <group2:Red xmi:id="29278" code="1" ref="6128" mask="6129" other="2188" />
    <group2:Yellow xmi:id="30300" code="4" ref="6398" mask="6400" other="2304" />
    <group2:Yellow xmi:id="30333" code="1" ref="6404" mask="6406" other="2308" />
    <group2:Yellow xmi:id="30394" code="5" ref="6426" mask="6429" other="2314" />
    <group2:Yellow xmi:id="30431" code="1" ref="6437" mask="6439" other="2318" />
    <group2:Yellow xmi:id="30468" code="6" ref="6447" mask="6450" other="2322" />
    <group2:Green xmi:id="79301" code="1" ref="2501" mask="2505" GreenType="NP"/>
    <group2:Green xmi:id="79306" code="6" ref="2505" mask="2506" GreenType="O"/>
    <group2:Green xmi:id="79311" code="1" ref="2507" mask="2520" GreenType="ADJP"/>
    <group2:Green xmi:id="79316" code="1" ref="2521" mask="2523" GreenType="PP"/>
    <group2:Green xmi:id="79321" code="1" ref="2524" mask="2542" GreenType="NP"/>
    <group3:Brown xmi:id="117792" code="7" ref="16421" mask="16426" id="0" max="0"/>
    <group3:Brown xmi:id="119483" code="1" ref="16486" mask="16497" id="0" />
    <group3:Brown xmi:id="117469" code="1" ref="16486" mask="16492" id="0" />
    <group3:Brown xmi:id="119364" code="8" ref="16493" mask="16497" id="0" />
    <group2:Grey xmi:id="137117" code="1" ref="143" mask="150" id="1" />
    <group2:Grey xmi:id="137131" code="1" ref="150" mask="151" id="2" />
    <group2:Grey xmi:id="137145" code="8" ref="152" mask="159" id="0"/>
    <group2:Grey xmi:id="137159" code="1" ref="152" mask="159" id="1" />
    <group3:Purple xmi:id="236545" id="0" category="R" argument="236523"/>
    <group3:Purple xmi:id="235624" id="0" category="A" argument="235612"/>
    <group3:Purple xmi:id="232638" id="0" category="A" argument="232632"/>
    <group3:Purple xmi:id="236845" id="0" category="A" argument="236821"/>
    <group3:Purple xmi:id="242015" id="0" category="C" argument="242003"/>
</xmi:XMI>

嗯。因此,正如Sajid在另一篇文章中提到的,group1和group2没有在发布的XML中声明。现在忽略这一点(您需要向我们提供关于如何声明或自己管理这些组的方法),有一种方法可以完成您所需要的工作—您可以通过以下方式获取特定节点的所有子节点,而不是通过多个名称获取元素的方法-

using System.Xml.Linq;    
XDocument xdoc = XDocument.Parse(xmlString);
List<XElement> elements = xdoc.Descendants("specificNodeName").DescendantNodes().OfType<XElement>();
此外,如果希望所有XElement名称作为字符串列表,则可以-

List<string> elementNames = xdoc.Root.DescendantNodes().OfType<XElement>().Select(x => x.Name).ToList()
List elementNames=xdoc.Root.degenantnodes().OfType().Select(x=>x.Name.ToList())
这将为您提供一个字符串列表-“黑色”、“白色”、“粉色”等

如果您想进一步了解如何在另一个元素中获取元素名称或获取唯一值,请参阅我的另一篇文章-

注意-您可能需要管理名称空间-这方面有很多帮助,例如

希望这有帮助

编辑

如果您仍然希望以自己的风格进行,则需要创建一个扩展方法-

public static class MyXmlExtensions
{
    public static List<XElement> GetElementsByName (this XElement main, List<string> requiredElements)
    {
        List<XElement> ElementsByName = new List<XElement>();
        foreach(string reqElement in requiredElements)
        {
            List<XElement> nodes = main.Descendants(reqElement);
            ElementsByName.AddRange(nodes);
        }
        return ElementsByName;
    }     
}
公共静态类MyXmlExtensions
{
公共静态列表GetElementsByName(此XElement主元素,列表requiredElements)
{
List ElementsByName=新列表();
foreach(requiredElements中的字符串reqElement)
{
列表节点=main.substands(reqElement);
ElementsByName.AddRange(节点);
}
返回ElementsByName;
}     
}
然后你就可以用它了-

List<string> requiredElements = new List<string> () { "group1:White", "group2:Blue", "group3:Brown" };
List<XElement> myElements = xdox.Root.GetElementsByName(requiredElements);
List requiredements=newlist(){“group1:White”、“group2:Blue”、“group3:Brown”};
List myElements=xdox.Root.GetElementsByName(requiredElements);
如果不想创建并传递列表,可以在扩展方法中直接使用params关键字-如果需要帮助,请告诉我

尝试xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace xmi = doc.Root.GetNamespaceOfPrefix("xmi");

            List<Group> groups = doc.Descendants().Where(x => x.GetPrefixOfNamespace(x.Name.NamespaceName).StartsWith("group")).Select(x => new Group()
            {
                name = x.Name.LocalName,
                color = x.Name.NamespaceName,
                id = (int?)x.Attribute(xmi + "id"),
                code = (int?)x.Attribute("code"),
                _ref = (int?)x.Attribute("ref"),
                mask = (int?)x.Attribute("mask"),
                whiteNumber = (int?)x.Attribute("WhiteNumber"),
                GreenType = (string)x.Attribute("GreenType"),
                max = (int?)x.Attribute("max"),
                category = (string)x.Attribute("category"),
                argument = (int?)x.Attribute("argument"),
                other = (int?)x.Attribute("other")
            }).ToList();
        }
    }
    public class Group
    {
        public string name { get; set; }
        public string color { get; set; }
        public int? xmiid { get; set; }
        public int? id { get; set; }
        public int? code { get; set; }
        public int? _ref { get; set; }
        public int? mask { get; set; }
        public int? whiteNumber { get; set; }
        public int? other { get; set; }
        public string GreenType { get; set; }
        public int? max { get; set; }
        public string category { get; set; }
        public int? argument { get; set; }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
XNamespace xmi=doc.Root.GetNamespaceOfPrefix(“xmi”);
List groups=doc.subjects()。其中(x=>x.GetPrefixOfNamespace(x.Name.NamespaceName).StartsWith(“组”)。选择(x=>newgroup()
{
name=x.name.LocalName,
color=x.Name.NamespaceName,
id=(int?)x.Attribute(xmi+“id”),
代码=(int?)x.Attribute(“代码”),
_ref=(int?)x.属性(“ref”),
掩码=(int?)x.Attribute(“掩码”),
whiteNumber=(int?)x.Attribute(“whiteNumber”),
GreenType=(字符串)x.Attribute(“GreenType”),
max=(int?)x.Attribute(“max”),
类别=(字符串)x.Attribute(“类别”),
参数=(int?)x.Attribute(“参数”),
其他=(int?)x.属性(“其他”)
}).ToList();
}
}
公共课组
{
公共字符串名称{get;set;}
公共字符串颜色{get;set;}
公共int?xmiid{get;set;}
公共int?id{get;set;}
公共int?代码{get;set;}
公共int?_ref{get;set;}
公共int?掩码{get;set;}
公共int?白数{get;set;}
公共int?其他{get;set;}
公共字符串绿色类型{get;set;}
公共int?max{get;set;}
公共字符串类别{get;set;}
公共int?参数{get;set;}
}
}

这是否回答了您的问题@Bandook不,没有,我问了另一个问题,问题是读取属性,这已经解决了,这是关于读取多个标记的另一个问题您需要获取
XElement
s的
列表?
public static class MyXmlExtensions
{
    public static List<XElement> GetElementsByName (this XElement main, List<string> requiredElements)
    {
        List<XElement> ElementsByName = new List<XElement>();
        foreach(string reqElement in requiredElements)
        {
            List<XElement> nodes = main.Descendants(reqElement);
            ElementsByName.AddRange(nodes);
        }
        return ElementsByName;
    }     
}
List<string> requiredElements = new List<string> () { "group1:White", "group2:Blue", "group3:Brown" };
List<XElement> myElements = xdox.Root.GetElementsByName(requiredElements);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XNamespace xmi = doc.Root.GetNamespaceOfPrefix("xmi");

            List<Group> groups = doc.Descendants().Where(x => x.GetPrefixOfNamespace(x.Name.NamespaceName).StartsWith("group")).Select(x => new Group()
            {
                name = x.Name.LocalName,
                color = x.Name.NamespaceName,
                id = (int?)x.Attribute(xmi + "id"),
                code = (int?)x.Attribute("code"),
                _ref = (int?)x.Attribute("ref"),
                mask = (int?)x.Attribute("mask"),
                whiteNumber = (int?)x.Attribute("WhiteNumber"),
                GreenType = (string)x.Attribute("GreenType"),
                max = (int?)x.Attribute("max"),
                category = (string)x.Attribute("category"),
                argument = (int?)x.Attribute("argument"),
                other = (int?)x.Attribute("other")
            }).ToList();
        }
    }
    public class Group
    {
        public string name { get; set; }
        public string color { get; set; }
        public int? xmiid { get; set; }
        public int? id { get; set; }
        public int? code { get; set; }
        public int? _ref { get; set; }
        public int? mask { get; set; }
        public int? whiteNumber { get; set; }
        public int? other { get; set; }
        public string GreenType { get; set; }
        public int? max { get; set; }
        public string category { get; set; }
        public int? argument { get; set; }

    }
}