Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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#:获取XML文档的所有节点_C#_Xml - Fatal编程技术网

C#:获取XML文档的所有节点

C#:获取XML文档的所有节点,c#,xml,C#,Xml,有没有一种简单的方法可以从xml文档中获取所有节点?我需要每个节点、childnode等等,来检查它们是否具有某些属性 还是我必须在文档中爬行,要求使用childnodes?查看LINQ to XML。这正是你需要的 例如,您可以使用SelectMany扩展名 但是,如果要检查值,可以使用LINQ创建where-语句。请参见此处: 很快: string xml = @" <parent> <child> <nested /&g

有没有一种简单的方法可以从xml文档中获取所有节点?我需要每个节点、childnode等等,来检查它们是否具有某些属性


还是我必须在文档中爬行,要求使用childnodes?

查看LINQ to XML。这正是你需要的

例如,您可以使用
SelectMany
扩展名

但是,如果要检查值,可以使用LINQ创建
where
-语句。

请参见此处:

很快:

 string xml = @"
    <parent>
      <child>
        <nested />
      </child>
      <child>
        <other>
        </other>
      </child>
    </parent>
    ";

  XmlReader rdr = XmlReader.Create(new System.IO.StringReader(xml));
  while (rdr.Read())
  {
    if (rdr.NodeType == XmlNodeType.Element)
    {
      Console.WriteLine(rdr.LocalName);
    }
  }
stringxml=@”
";
XmlReader rdr=XmlReader.Create(newsystem.IO.StringReader(xml));
while(rdr.Read())
{
if(rdr.NodeType==XmlNodeType.Element)
{
Console.WriteLine(rdr.LocalName);
}
}

在LINQ to XML中,它非常简单:

XDocument doc = XDocument.Load("test.xml"); // Or whatever
var allElements = doc.Descendants();
因此,要查找具有特定属性的所有元素,例如:

var matchingElements = doc.Descendants()
                          .Where(x => x.Attribute("foo") != null);
假设你想要所有元素。如果需要所有节点(包括文本节点等,但不包括作为单独节点的属性),则可以使用

编辑:LINQtoXML中的名称空间很好。您可以使用:

var matchingElements = doc.Descendants()
                          .Where(x => x.Attribute(XNamespace.Xmlns + "aml") != null);
或者对于不同的命名空间:

XNamespace ns = "http://some.namespace.uri";

var matchingElements = doc.Descendants()
                          .Where(x => x.Attribute(ns + "foo") != null);
将返回平面枚举中的所有节点。

public void AddWithChildren(XmlNode xnod,Int32 intLevel)/,XmlDocument xmlDoc
  protected void Page_Load(object sender, EventArgs e)
  {

            XmlDocument document = new XmlDocument();

            string xmlStr;
            using (var wc = new WebClient())
            {
                xmlStr = wc.DownloadString("test.xml");
            }

            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xmlStr);

            XmlNode xnod = xmlDoc.DocumentElement;

           AddWithChildren(xnod, 1);
 }
{ 列表项=新列表(); xmlnodexnodworking; strIndent=新字符串('-',2*intLevel); String strIndent1=新字符串('@',2*intLevel); if(xnod.NodeType==XmlNodeType.Element) { 添加(新的ListXML(strIndent+xnod.Name,strIndent+xnod.Name,“”)); XmlNamedNodeMap mapAttributes=xnod.Attributes; foreach(mapAttributes中的XmlNode xnodAttribute) { 添加(新的ListXML(strIndent1+xNodeAttribute.Name,strIndent1+xNodeAttribute.Name,“”); } if(xnod.HasChildNodes) { xnodWorking=xnod.FirstChild; while(xnodWorking!=null) { AddWithChildren(XNodeWorking,intLevel+1); xnodWorking=xnodWorking.NextSibling; } } } }
在我看来,最简单的解决方案是使用XPath。如果您有.NET 2:

var testDoc = new XmlDocument();
testDoc.LoadXml(str);
var tmp = testDoc.SelectNodes("//*"); // match every element

与XDocument和其他类似DOM的方法相比,这个方法通常非常非常快。@杰夫:你的意思是使用索引器而不是方法调用来获取属性吗?如果是,请修复。如果不是,我很困惑……是的,这就是我的意思。:)杰出的有一点不相关,但如何检查名称中有名称空间的属性呢?比如:xmlns:aml?它说我的属性名中不能有:。如果需要检查某些属性,请查看链接,不需要遍历每个节点(文本节点、文档节点、注释节点)。只需遍历每个元素节点或每个属性节点(即使用LINQ或XSLT)。元素节点是唯一具有属性的节点类型。此文档有多大?Ie是否值得优化?没有解释的代码转储很少有帮助。请考虑在您的答案中添加一些上下文。用属性名称获取所有节点:var pnNoS= TestDoc。SelectNodes(“//*[AtjtItAtMaMe]”);
var testDoc = new XmlDocument();
testDoc.LoadXml(str);
var tmp = testDoc.SelectNodes("//*"); // match every element
string AttrNameerr = "err";//find error code in xml
XmlReader rdr = XmlReader.Create(new stem.IO.StringReader(somesXMLtring));//somesXMLtring is xml in string variable we want to find attribute in.
while (rdr.Read())
{
    if (rdr.NodeType == XmlNodeType.Element)
    {
      //Found the new element, now check if the required attribute is present or not. if not, ignore, if yes then display the same
      string val = rdr.GetAttribute(AttrNameerr);//AttrNameerr is name of attribute we need to get value of which. here we are searching for error code stored as value of 'err' attribute

        if (val != null)
          textBox.Text = strResult = "error = " + rdr.GetAttribute(AttrNameerr);

    }
}