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# xml中的元素始终具有空值_C#_Xml_Windows Phone 7 - Fatal编程技术网

C# xml中的元素始终具有空值

C# xml中的元素始终具有空值,c#,xml,windows-phone-7,C#,Xml,Windows Phone 7,我想读取我在这里编写的xml文件 <?xml version="1.0" encoding="utf-8"?> <ReyPatch> <Key name="{8880-089B7A97D4B7}" new="true"> <Value name="" type="string" patchedValue="5lpha" /> <Value name="LayID" type="dword" patchedValue="2

我想读取我在这里编写的xml文件

<?xml version="1.0" encoding="utf-8"?>
<ReyPatch>

  <Key name="{8880-089B7A97D4B7}" new="true">
    <Value name="" type="string" patchedValue="5lpha" />
    <Value name="LayID" type="dword" patchedValue="2" />
    <Value name="Usons" type="dword" patchedValue="1" />
    <Value name="IsBaition" type="dword" patchedValue="0" />
    <Value key="key" name="Type" type="dword" patchedValue="2036" />
<Value key="KeyHars" name="Count" type="dword" patchedValue="0" />
  </Key>
<Key name="BBBE-A957C7628109}" new="true">
    <Value name="" type="string" patchedValue="4pha" />
    <Value name="LayD" type="dword" patchedValue="2" />
    <Value name="Utons" type="dword" patchedValue="1" />
    <Value name="IsBfinition" type="dword" patchedValue="0" />
    <Value key="Keys\0" name="Type" type="dword" patchedValue="2807" />
    <Value key="Keys\0" name="Text" type="string" patchedValue="2" />
    <Value key="Keys\1" name="Type" type="dword" patchedValue="2097" />
    <Value key="Keers" name="Count" type="dword" patchedValue="0" />
  </Key>
</ReyPatch>
我一直在测试,但我没有找到任何方法
如何修复此问题?

名称
新建
类型
修补值
是属性,而不是元素。您需要使用
属性
方法,而不是
元素
。为了防止属性丢失时出现
NullReferenceException
,您应该将属性强制转换为
string
,而不是使用
ToString
Value

        .Select(e =>
            new Key
            {
                name = (string)e.Attribute("name"),
               IsNew =Convert.ToBoolean((string)e.Attribute("new")),
                v = e.
                Elements("Value").Select(i =>

                    new Value
                    {
                        name = (string)i.Attribute("name"),
                        type = (string)i.Attribute("type"),
                        patchedValue = (string)i.Attribute("patchedValue")
                    }).ToArray()
            }).ToArray();

您将获得异常,因为您将获得xml的所有子体。您应该使用
子体(“键”)
。否则,将选择的第一个元素是
元素,它没有元素
,并且您会在
e.element(“name”).ToString()上获得异常

@juharr是正确的,您正在尝试获取元素而不是属性。看到区别了吗

整个解析应该如下所示(我强烈建议使用节点的强制转换,而不是获取它们的值):

我建议您使用PascalCase进行属性命名,以及更具描述性的属性。例如

public class Key
{
   public string Name { get; set; }
   public bool IsNew { get; set; }
   public Value[] Values { get; set; }
}

您正在查找不存在的元素。您应该查看属性。当我注释name=e.Attribute(“name”).ToString(),IsNew=Convert.ToBoolean(e.Attribute(“new”).Value时,这部分工作完成了一半,但没有全部工作,但当我再次取消注释时,我有NullReferenceException:(
 doc.Descendants("Key")
    .Select(key => new Key()
    {
        name = (string)key.Attribute("name"),
        IsNew = (bool)key.Attribute("new"),
        v = key.Elements()
                .Select(value => new Value()
                {
                    name = (string)value.Attribute("name"),
                    type = (string)value.Attribute("type"),
                    patchedValue = (string)value.Attribute("patchedValue")
                }).ToArray()
    }).ToArray();
public class Key
{
   public string Name { get; set; }
   public bool IsNew { get; set; }
   public Value[] Values { get; set; }
}