xml中的元素始终具有空值

时间:2013-01-16 11:47:29

标签: 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" />
    <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

Uri url = new Uri("p.xml", UriKind.Relative);
            StreamResourceInfo resourceStream = Application.GetResourceStream(url);
            var doc = XDocument.Load(resourceStream.Stream);
            var newCookies = doc
            .Descendants()
            .Select(e =>
                new Key
                {
                    name = e.Element("name").ToString(),
                   IsNew =Convert.ToBoolean( e.Element("new").Value),
                    v = e.
                    Elements("Value").Select(i =>

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

我一直在测试,但我没有找到任何办法 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

namenewtypepatchedValue是属性,而不是元素。您需要使用Attribute方法而不是Element。为了防止NullReferenceException缺少该属性,您应该将该属性转换为string,而不是使用ToStringValue。:

        .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();

答案 1 :(得分:0)

您将获得异常,因为您获得了xml的所有后代。您应该使用.Descendants("Key")。否则,将被选中的第一个元素是<ReyPatch>元素,它没有元素<name>,并且您在e.Element("name").ToString()上获得例外。

@juharr是正确的,你试图获取元素而不是属性。请参阅差异XML Elements vs. Attributes

整个解析应该看起来像(我强烈建议使用节点的转换而不是获取它们的值):

 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();

我建议您使用PascalCase进行属性命名和更具描述性的属性。 E.g。

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