Is XML Elements Attribute value always data type of string?

依然范特西╮ 提交于 2019-12-11 01:37:23

问题


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?!


回答1:


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.



来源:https://stackoverflow.com/questions/26379256/is-xml-elements-attribute-value-always-data-type-of-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!