Character ":", the hexadecimal value 0x3A, cannot be included in the name "

I have some code that will read some XML files. I tried different ways to solve this problem, but could not. I also tried to make the code as follows:

Namespace my = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-01-11T08:31:30"; XElement myEgitimBilgileri = XDocument.Load(@"C:\25036077.xml").Element("my:"+ "Egitim_Bilgileri"); 

But the same mistake all the time. Here is the original:

 private void button2_Click(object sender, EventArgs e) { XElement myEgitimBilgileri = XDocument.Load(@"C:\25036077.xml").Element("my:Egitim_Bilgileri"); if (myEgitimBilgileri != null) { foreach (XElement element in myEgitimBilgileri.Elements()) { Console.WriteLine("Name: {0}\tValue: {1}", element.Name, element.Value.Trim()); } } Console.Read(); } 

Here is the path to my xml file:

 <my:Egitim_Bilgileri> <my:egitimler_sap> <my:sap_eduname></my:sap_eduname> <my:sap_edufaculty></my:sap_edufaculty> <my:sap_eduprofession></my:sap_eduprofession> <my:sap_diplomno></my:sap_diplomno> <my:sap_edulevel></my:sap_edulevel> <my:sap_edustartdate xsi:nil="true"></my:sap_edustartdate> <my:sap_eduenddate xsi:nil="true"></my:sap_eduenddate> </my:egitimler_sap> </my:Egitim_Bilgileri> 

and this is the path of my namespace in XML

 xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-01-11T08:31:30" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-01-23T00:43:17" 
+5
source share
1 answer

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); 
+12
source

All Articles