将 GetElementsByTagName 与多标签一起使用
using GetElementsByTagName with multi tags
我的 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>
有什么方法可以一次性 select 所有标签的名称
像
xmlnode = xmldoc.GetElementsByTagName("group1:White, group2:Blue, group3:Brown");
好的。正如 Sajid 在您的其他 post 中提到的那样 - 组 1 和组 2 未在 posted XML 中声明。现在忽略它(您需要向我们提供有关如何声明这些组或由您自己管理它的方法),有一种方法可以满足您的需要 - 而不是通过多个名称获取元素的方法,您可以通过 -
获取特定节点的所有子节点
using System.Xml.Linq;
XDocument xdoc = XDocument.Parse(xmlString);
List<XElement> elements = xdoc.Descendants("specificNodeName").DescendantNodes().OfType<XElement>();
如果您想要根内的所有 XElements -
List<XElement> elements = xdoc.Root.DescendantNodes().OfType<XElement>();
这样做也会给你 "cas:Null" 标签,但你可以用一个简单的条件忽略它。
如果需要获取属性,可以-
foreach(XElement element in elements)
{
string attrValue = element.Attribute("attributeNameHere").Value;
}
此外,如果您希望所有 XElement Names 作为字符串列表,那么您可以 -
List<string> elementNames = xdoc.Root.DescendantNodes().OfType<XElement>().Select(x => x.Name).ToList()
这将为您提供一个字符串列表 - "Black"、"White"、"Pink" 等
如果您想进一步了解如何在另一个元素中获取元素名称或获取唯一值,请查看我的其他文章 post -
注意 - 您可能需要管理名称空间 - 在 SO 中有很多帮助,例如How to parse XML with namespace
希望这对您有所帮助。
编辑
如果您仍想按照自己的风格进行操作,则需要创建一个扩展方法,达到这种效果 -
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);
如果您不想创建列表并传递它,您可以直接在扩展方法中使用 params 关键字 - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/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; }
}
}
我的 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>
有什么方法可以一次性 select 所有标签的名称 像
xmlnode = xmldoc.GetElementsByTagName("group1:White, group2:Blue, group3:Brown");
好的。正如 Sajid 在您的其他 post 中提到的那样 - 组 1 和组 2 未在 posted XML 中声明。现在忽略它(您需要向我们提供有关如何声明这些组或由您自己管理它的方法),有一种方法可以满足您的需要 - 而不是通过多个名称获取元素的方法,您可以通过 -
获取特定节点的所有子节点using System.Xml.Linq;
XDocument xdoc = XDocument.Parse(xmlString);
List<XElement> elements = xdoc.Descendants("specificNodeName").DescendantNodes().OfType<XElement>();
如果您想要根内的所有 XElements -
List<XElement> elements = xdoc.Root.DescendantNodes().OfType<XElement>();
这样做也会给你 "cas:Null" 标签,但你可以用一个简单的条件忽略它。
如果需要获取属性,可以-
foreach(XElement element in elements)
{
string attrValue = element.Attribute("attributeNameHere").Value;
}
此外,如果您希望所有 XElement Names 作为字符串列表,那么您可以 -
List<string> elementNames = xdoc.Root.DescendantNodes().OfType<XElement>().Select(x => x.Name).ToList()
这将为您提供一个字符串列表 - "Black"、"White"、"Pink" 等
如果您想进一步了解如何在另一个元素中获取元素名称或获取唯一值,请查看我的其他文章 post -
注意 - 您可能需要管理名称空间 - 在 SO 中有很多帮助,例如How to parse XML with namespace
希望这对您有所帮助。
编辑
如果您仍想按照自己的风格进行操作,则需要创建一个扩展方法,达到这种效果 -
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);
如果您不想创建列表并传递它,您可以直接在扩展方法中使用 params 关键字 - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/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; }
}
}