Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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# Can';找不到命名元素_C#_Linq To Xml - Fatal编程技术网

C# Can';找不到命名元素

C# Can';找不到命名元素,c#,linq-to-xml,C#,Linq To Xml,有东西没有用此代码为我单击: var cfgDoc = XElement.Load(_cfgFile); foreach (var x in cfgDoc.Elements("property")) { Console.WriteLine(x); } 我希望能够只输出名为property但得到zilch的节点。如果我不使用XName参数,我会得到文档(如下所示),这至少表明它正在正确加载 我错过了什么 闭上 真的 第二次尝试 var cfgDoc = XElement.Load(_

有东西没有用此代码为我单击:

var cfgDoc = XElement.Load(_cfgFile);
foreach (var x in cfgDoc.Elements("property"))
{
    Console.WriteLine(x);
}
我希望能够只输出名为
property
但得到zilch的节点。如果我不使用XName参数,我会得到文档(如下所示),这至少表明它正在正确加载

我错过了什么


闭上
真的
第二次尝试

var cfgDoc = XElement.Load(_cfgFile);
XNamespace ns = "urn:nhibernate-configuration-2.2"; 
var properties = cfgDoc.Elements(ns + "property");
Assert.That(properties.Count() > 0);
foreach (var x in cfgDoc.Elements(ns + "property")) 
{
    Console.WriteLine(x);
}

您尚未提供子元素有效继承的命名空间。试试这个:

var cfgDoc = XElement.Load(_cfgFile);
XNamespace ns = "urn:nhibernate-configuration-2.2";
foreach (var x in cfgDoc.Elements(ns + "property"))
{
    Console.WriteLine(x);
}
其中一个(或两个)可以做到:

cfgDoc.Descendants(ns + "property")


(Jon也是对的,您肯定需要名称空间)

您确定要加载文件/xml吗?检查您的
\u cfgFile
值。你的第二次尝试对我有用:

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

namespace XElementMadness
{
    class Program
    {
        static void Main(string[] args)
        {
            var xml = "<session-factory xmlns=\"urn:nhibernate-configuration-2.2\"><property name=\"connection.release_mode\">on_close</property><property name=\"show_sql\">true</property></session-factory>";

            var cfgDoc = XElement.Load(new StringReader(xml));
            XNamespace ns = "urn:nhibernate-configuration-2.2";
            var properties = cfgDoc.Elements(ns + "property");

            foreach (var x in cfgDoc.Elements(ns + "property"))
            {
                Console.WriteLine(x);
            }

            Console.ReadLine();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml.Linq;
使用System.IO;
名称空间像素性
{
班级计划
{
静态void Main(字符串[]参数)
{
var xml=“on_closetrue”;
var cfgDoc=XElement.Load(新的StringReader(xml));
XNamespace ns=“urn:nhibernate-configuration-2.2”;
var属性=cfgDoc.Elements(ns+“属性”);
foreach(cfgDoc.Elements中的变量x(ns+“属性”))
{
控制台写入线(x);
}
Console.ReadLine();
}
}
}
哪些产出:

<property name="connection.release_mode" xmlns="urn:nhibernate-configuration-2.2 ">on_close</property> <property name="show_sql" xmlns="urn:nhibernate-configuration-2.2">true</property>
on_close true

你好,乔恩。听起来很有逻辑,但结果是一样的——Zlich(我在文章的末尾附加了代码)。我打算提出这个建议,所以我会解释为什么这些会产生不同。当您只执行XElement.Load时,返回的变量表示根节点,它与根元素不同。在示例文件中,
会话工厂
是根元素。根节点是一个抽象概念,表示文档的整体,并被视为根(
会话工厂
)元素的父元素。要从根节点到
属性
元素,您必须首先通过
会话工厂
。根节点也可以工作,但就我所见,它只适用于子节点。很接近,足以回答我的问题!干杯。My _cfgFile是指向xml文件的路径,我想从结果来看,它的处理方式与在代码段中构建它的方式不同。可能与西蒙的评论有关。干杯
<property name="connection.release_mode" xmlns="urn:nhibernate-configuration-2.2 ">on_close</property> <property name="show_sql" xmlns="urn:nhibernate-configuration-2.2">true</property>