Reputation: 13
i want to read the xml file that i write it here
<?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>
i wrote this code but always has 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();
}
i test all of way and i don't found any way to do it how can i fix this?
Upvotes: 0
Views: 114
Reputation: 236268
You are getting exception because you are getting all descendants of your xml. You should use .Descendants("Key")
. Otherwise first element that will be selected is <ReyPatch>
element, which do not have element <name>
and you get exception on e.Element("name").ToString()
.
@juharr is correct, you are trying to get elements instead of attributes. See difference XML Elements vs. Attributes
Whole parsing should look like (I strongly recommend to use casting of nodes instead of getting their values):
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();
And I suggest you to use PascalCase for properties naming, and more descriptive properties. E.g.
public class Key
{
public string Name { get; set; }
public bool IsNew { get; set; }
public Value[] Values { get; set; }
}
Upvotes: 0
Reputation: 174397
name
, new
, type
and patchedValue
are attributes, not elements. You need to use the Attribute
method instead of Element
. And to prevent a NullReferenceException
when that attribute is missing, you should just cast the attribute to a string
instead of using ToString
or 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();
Upvotes: 1