我想讀的XML文件,我把它寫在這裏在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();
}
我測試所有的方式並且我沒有找到任何方法去做 我該如何解決這個問題?
您正在尋找不存在的元素。您應該查看屬性。 – juharr