IremadzeArchil19910311
IremadzeArchil19910311

Reputation: 956

Is XML Elements Attribute value always data type of string?

When I create a XML Document using LINQ, when I add some XElement to Root element with some Attributes and when I read that document`s XElement using LINQ, the returned value of XAttributes.Value is string by default!
In order to assign this value to bool type of variable, it is necessary to call function "Convert.ToBoolean()"

  XDocument Xd = new XDocument(new XElement("Numbers"));
  Xd.Root.Add(new XElement("13", new XAttribute("Name", "13")
                               , new XAttribute("IsEvenNumber", false)
                               , new XAttribute("HowManyDevidersItHas", 2)));
  Xd.Save(@"C:\XDocument.xml");
  bool b1 = Convert.ToBoolean(XD1.Root.Element("13").Attribute("IsEvenNumber").Value);
  ...

As you can see:
the value of XAttribute called "Name" must be as a long type!
the value of XAttribute called "IsEvenNumber" must be a bool type!

I need to know: Is it possible to create an XElement with some XAttributes, save it, read it again and assign its XAttributes.Value to some bool type variable without calling " Convert.ToBoolean()" function?!

Upvotes: 2

Views: 2282

Answers (1)

KnottytOmo
KnottytOmo

Reputation: 546

Unfortunately not, the XElement.Value property appears to be a string.

http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.value(v=vs.90).aspx

Thus you will always have to cast the value to your data type.

Upvotes: 2

Related Questions