Element("my:" + "Egitim_Bilgileri") matches Element("my:Egitim_Bilgileri") , which is interpreted as an element named "my: Egitim_Bilgileri" in the default namespace (there is an implicit conversion from string to XName ).
However : not valid in the name of the element (outside the separation of the namespace) and therefore will throw an exception at runtime.
Instead, the code should be Element(my + "Egitim_Bilgileri") , where my is an XNamespace object. The operator of the XNamespace + object when specifying a string as the second operand results in an XName object, which can then be used with Element(XName) .
For instance:
XNamespace my = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-01-11T08:31:30"; XDocument doc = XDocument.Load(@"C:\25036077.xml"); // The following variable/assignment can be omitted, // it is to show the + operator of XNamespace and how it results in XName XName nodeName = my + "Egitim_Bilgileri"; XElement myEgitimBilgileri = doc.Element(nodeName);
Happy coding ... and condolences over the need to deal with InfoPath :)
I prefer to use XPath in most cases. Among other things, it makes it easy to select nested nodes and avoid the need to βcheck zeroβ at each level, which may be required with the node.Element(x).Element(y) constructs.
using System.Xml.XPath; // for XPathSelectElement extension method XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable()); ns.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-01-11T08:31:30") // Note that there is no XName object. Instead the XPath string is parsed // and namespace resolution happens via the XmlNamespaceManager XElement myEgitimBilgileri = doc.XPathSelectElement("/my:myFields/my:Egitim_Bilgileri", ns);
user166390
source share