0

我想阅读我在这里写的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();
        }

我一直在测试,但我没有找到任何方法可以解决这个问题?

4

2 回答 2

1

name, new,typepatchedValue是属性,而不是元素。您需要使用Attribute方法而不是Element. 并且为了防止 aNullReferenceException当该属性丢失时,您应该将属性转换为 astring而不是使用ToStringor 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();
于 2013-01-16T11:54:49.707 回答
0

你得到了例外,因为你得到了你的 xml 的所有后代。你应该使用.Descendants("Key"). 否则,将选择的第一个元素是<ReyPatch>元素,它没有元素<name>,你会得到异常e.Element("name").ToString()

@juharr 是正确的,您正在尝试获取元素而不是属性。查看XML 元素与属性的区别

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

 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 进行属性命名和更具描述性的属性。例如

public class Key
{
   public string Name { get; set; }
   public bool IsNew { get; set; }
   public Value[] Values { get; set; }
}
于 2013-01-16T11:58:11.393 回答