Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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/2/.net/25.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中的.NET搜索子元素_C#_.net_Xml_List - Fatal编程技术网

C# xml中的.NET搜索子元素

C# xml中的.NET搜索子元素,c#,.net,xml,list,C#,.net,Xml,List,我正在尝试使用Linq将XML文件的内容加载到自定义类型列表中,请遵循官方microsoft dotNet api中的说明: 我的xml文件如下所示: 导入代码与上面链接的示例中的代码基本相同 publicstaticvoidimportdirectives() { //从源文件创建XML元素。 XElement xTree=XElement.Load(AppDomain.CurrentDomain.BaseDirectory+“directions.xml”); //创建元素的可枚举集合

我正在尝试使用Linq将XML文件的内容加载到自定义类型列表中,请遵循官方microsoft dotNet api中的说明:

我的xml文件如下所示:


导入代码与上面链接的示例中的代码基本相同

publicstaticvoidimportdirectives()
{
//从源文件创建XML元素。
XElement xTree=XElement.Load(AppDomain.CurrentDomain.BaseDirectory+“directions.xml”);
//创建元素的可枚举集合。
IEnumerable elements=xTree.elements();
//计算book对象中的每个元素并设置值。
foreach(元素中的元素el)
{
指令dir=新指令();
dir.directive=el.Attribute(“directive”).Value;
IEnumerable props=el.Elements();
foreach(道具中的XElement p)
{
if(p.Name.ToString().ToLower()=“响应”)
{
dir.response=p.值;
}
}
目录添加(目录);
}
}
如果删除根元素,代码运行良好,但如果添加根元素,则只将其添加到列表中。 我更喜欢使用根元素,以使XML看起来更合适。
如何使用此代码访问根元素中的元素?

如果您确定XML模式将被修复,则可以直接访问XML元素的属性值

我附上了一个样本代码

public static void ImportDirectives()
    {
        string fileName = AppDomain.CurrentDomain.BaseDirectory + "directives.xml";
        // Create XML elements from a source file.
        XElement xTree = XElement.Load(fileName);

        // Create an enumerable collection of the elements.
        IEnumerable<XElement> elements = xTree.Elements();

        // Evaluate each element and set set values in the book object.
        foreach (XElement el in elements)
        {
            string directive = el.Attribute("directive").Value;
            string response = el.Attribute("response").Value;

            Console.WriteLine(directive + ":" + response);
        }
    }
publicstaticvoidimportdirectives()
{
字符串文件名=AppDomain.CurrentDomain.BaseDirectory+“directions.xml”;
//从源文件创建XML元素。
XElement xTree=XElement.Load(文件名);
//创建元素的可枚举集合。
IEnumerable elements=xTree.elements();
//计算book对象中的每个元素并设置值。
foreach(元素中的元素el)
{
字符串指令=el.属性(“指令”).值;
字符串响应=el.属性(“响应”).值;
Console.WriteLine(指令+”:“+响应);
}
}

当您添加root时,然后
xml
类似

<Root>
  <directives>
    <dir directive="Question" response="Response"></dir>
    <dir directive="Q2" response="Response2"></dir>
    <dir directive="Q3" response="Response3"></dir>
  </directives>
</Root>
再次获取
元素()
,然后您将获得节点

<dir directive="Question" response="Response"></dir>

然后访问属性和值

      public static void ImportDirectives()
        {
            // Create XML elements from a source file.
            XElement xTree = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "directives.xml");

            // Create an enumerable collection of the elements.
            IEnumerable<XElement> elements = xTree.Elements();

            // Evaluate each element and set set values in the book object.
            foreach (XElement el in elements.Elements())
            {
                Directive dir = new Directive();
                dir.directive = el.Attribute("directive").Value;
                IEnumerable<XElement> props = el.Elements();
                foreach (XElement p in props)
                {
                    if (p.Name.ToString().ToLower() == "response")
                    {
                        dir.response = p.Value;
                    }
                }
                Dir.Add(dir);
            }
        }
publicstaticvoidimportdirectives()
{
//从源文件创建XML元素。
XElement xTree=XElement.Load(AppDomain.CurrentDomain.BaseDirectory+“directions.xml”);
//创建元素的可枚举集合。
IEnumerable elements=xTree.elements();
//计算book对象中的每个元素并设置值。
foreach(elements.elements()中的XElement el)
{
指令dir=新指令();
dir.directive=el.Attribute(“directive”).Value;
IEnumerable props=el.Elements();
foreach(道具中的XElement p)
{
if(p.Name.ToString().ToLower()=“响应”)
{
dir.response=p.值;
}
}
目录添加(目录);
}
}
      public static void ImportDirectives()
        {
            // Create XML elements from a source file.
            XElement xTree = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "directives.xml");

            // Create an enumerable collection of the elements.
            IEnumerable<XElement> elements = xTree.Elements();

            // Evaluate each element and set set values in the book object.
            foreach (XElement el in elements.Elements())
            {
                Directive dir = new Directive();
                dir.directive = el.Attribute("directive").Value;
                IEnumerable<XElement> props = el.Elements();
                foreach (XElement p in props)
                {
                    if (p.Name.ToString().ToLower() == "response")
                    {
                        dir.response = p.Value;
                    }
                }
                Dir.Add(dir);
            }
        }