Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中发现XML子节点#_C#_Xml_Xml Parsing - Fatal编程技术网

C# 在C中发现XML子节点#

C# 在C中发现XML子节点#,c#,xml,xml-parsing,C#,Xml,Xml Parsing,我需要将XML与公共父节点结合起来,但与不同的子节点结合起来。一旦我得到它,我需要获取子节点的标记名,并使用这些名称作为标题。在以下示例中,所有传入的XML将按如下方式包装: <customers> <customer> ...varying child nodes that do not have child nodes themselves </customer> </customers> …不同的子节点本身没

我需要将XML与公共父节点结合起来,但与不同的子节点结合起来。一旦我得到它,我需要获取子节点的标记名,并使用这些名称作为标题。在以下示例中,所有传入的XML将按如下方式包装:

<customers>
    <customer>
       ...varying child nodes that do not have child nodes themselves
    </customer>
</customers>

…不同的子节点本身没有子节点
我发现这是有效的:

List<string> headerList = new List<string>();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(someXML);

XmlNodeList xnl = xmlDoc.SelectNodes("customers/customer");

foreach (XmlNode xn in xnl)
{
    for (int x = 0; x < xn.ChildNodes.Count; x++)
    {
        headerList.Add(xn.ChildNodes[x].Name.ToString());
    }
}
List headerList=newlist();
XmlDocument xmlDoc=新的XmlDocument();
LoadXml(someXML);
XmlNodeList xnl=xmlDoc.SelectNodes(“客户/客户”);
foreach(xnl中的xmlnodexn)
{
对于(int x=0;x
有更好的方法吗


提前感谢。

这应该可以解决问题

XDocument doc = XDocument.Load(someXML);
var headerList = doc.Descendants("customer").Elements().Select(x => x.Name);

不一定“更好”,但我想它更简洁一些。

您有没有研究过XmlReader或XmlSerializer?同意,林克是你的朋友,在这里!