C # xml element () select element which is not defined
I 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 get an object not set to an object error instance when searching for Name = "Sim". I understand this is happening because there was no set in xml fields like Tail, MinLength, MaxLength, etc. This is logical, because my xsd detects that these minoccurrences are set to 0. In theory, there will be some fields in the xml that have some fields and others don't and have required fields.
is there any way to check if fields exist and if they do not assign N2NField values ββto the object as null for those properties? I don't want to be forced to make all fields required 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; }
}
source to share
I've been messing around with this idea already and it seems to work for me
XElement xdb = XElement.Load(XMLPath);
XNamespace NameSpace = xdb.Name.Namespace;
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.Descendants(NameSpace + "Default").Any() ? root.Element(NameSpace + "Default").Value : null,
Head = root.Descendants(NameSpace+"Head").Any() ? root.Element(NameSpace + "Head").Value : null,
Tail = root.Descendants(NameSpace+"Tail").Any() ? root.Element(NameSpace + "Tail").Value : null,
MinLength = root.Descendants(NameSpace+"MinLength").Any() ? int.Parse(root.Element(NameSpace + "MinLength").Value) : -1,
MaxLength = root.Descendants(NameSpace+"MaxLength").Any() ? int.Parse(root.Element(NameSpace + "MaxLength").Value) : -1
}
).First();
checking if the element exists first with Descendants (). Any () allows me to assign a default code-level value if node doesn't exist.
source to share
You don't seem to have some nodes in your XML; note I removed the property .Value
and added a line to the line.
var xe = (from root in xdb.Descendants(NameSpace + "Field")
where root.Attribute("Name").Value.Equals("Sim")
select new N2NField
{
Name = (string)root.Attribute("Name"),
ID = int.Parse((string)root.Element(NameSpace + "ID") ?? "0"),
Default = (string)root.Element(NameSpace + "Default"),
Head = (string)root.Element(NameSpace + "Head"),
Tail = (string)root.Element(NameSpace + "Tail"),
MinLength = int.Parse((string)root.Element(NameSpace + "MinLength") ?? "0"),
MaxLength = int.Parse((string)root.Element(NameSpace + "MaxLength") ?? "0")
}).First();
NTN
source to share
This is more efficient than using the Descendants () method. Any (). This just checks if the one you are looking for to determine if it is null and if it does not assign a value.
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") == null ? root.Element(NameSpace + "Default").Value : null,
Head = root.Element(NameSpace + "Head") == null ? root.Element(NameSpace + "Head").Value : null,
Tail = root.Element(NameSpace + "Tail") == null ? root.Element(NameSpace + "Tail").Value : null,
MinLength = root.Element(NameSpace + "MinLength") == null ? int.Parse(root.Element(NameSpace + "MinLength").Value) : -1,
MaxLength = root.Element(NameSpace + "MaxLength") == null ? int.Parse(root.Element(NameSpace + "MaxLength").Value) : -1
}).First();
source to share
I know this is a very old answer, but I came to this question from Google and found a great solution for my problem.
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.Elements(NameSpace + "Default").Any ? root.Element(NameSpace + "Default").Value : null,
Head = root.Elements(NameSpace + "Head").Any ? root.Element(NameSpace + "Head").Value : null,
}).First();
This worked in my case, where subsequent Brians exams gave me the same error as you.
source to share