hi
i have created the following xml
<Fields xmlns="http://tempuri.org/XMLSchema1.xsd:Fields">
<Field Name="ServiceProviderName">
<ID>0</ID>
</Field>
<Field Name="TransactionNumber">
<ID>1</ID>
<Padding Length="8" PadChar="0"/>
<MaxLength>10</MaxLength>
</Field>
<Field Name="Sim">
<ID>2</ID>
<Head>8927</Head>
<Padding Length="15" PadChar="0"/>
</Field>
</Fields>
and i am trying to assign this to an object using linq. i have defined an object called N2NField
var xe = (from root in xdb.Descendants(NameSpace + "Field")
where root.Attribute("Name").Value.Equals(Name)
select new N2NField
{
Name = root.Attribute("Name").Value,
ID = int.Parse(root.Element(NameSpace+"ID").Value),
Default = root.Element(NameSpace + "Default").Value,
Head = root.Element(NameSpace + "Head").Value,
Tail = root.Element(NameSpace + "Tail").Value,
MinLength = int.Parse(root.Element(NameSpace + "MinLength").Value),
MaxLength = int.Parse(root.Element(NameSpace + "MaxLength").Value)
}).First();
i am getting an object not set to an instance of object error when searching for Name="Sim". I understand that this is happening because in the xml fields like Tail, MinLength, MaxLength, etc have not been set. This is logical because my xsd defines those minoccurrences are set to 0. in theory there will be some fields in the xml that have some fields while not having others and having mandatory fields.
is there any way to check to see if the fields exist and if they don't assign the object N2NField values as null for those properties? i don't want to be forced to make all the fields mandatory in the xsd. any ideas?
Edit - N2N Field Class
public class N2NField {
public string Name { get; set; }
public int ID { get; set; }
public string Default { get; set; }
public string Head { get; set; }
public string Tail { get; set; }
public int MinLength { get; set; }
public int MaxLength { get; set; }
}