6

I have a code which will read some xml file(s). I tried different ways to solve this problem, but couldn't. Also I tried to code like this:

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 all the time the same mistake. 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 a path of 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"
Abdulyar
  • 61
  • 1
  • 1
  • 3
  • See also [The ':' character, hexadecimal value 0x3A, cannot be included in a name](http://stackoverflow.com/questions/2575546/the-character-hexadecimal-value-0x3a-cannot-be-included-in-a-name). –  Mar 29 '14 at 23:43

1 Answers1

14

The code Element("my:" + "Egitim_Bilgileri") is the same as Element("my:Egitim_Bilgileri") which is taken to mean the element named "my:Egitim_Bilgileri" in the default namespace (there is an implicit conversion from string to XName).

However, : is invalid in an element name (outside of the namespace separation) and thus will result in a run-time exception.

Instead, the code should be Element(my + "Egitim_Bilgileri") where my is an XNamespace object. The XNamespace object's + operator, when given a string as the second operand, results in an XName object that can then be used with the Element(XName) method.

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 for having to deal with InfoPath :)


I prefer to use XPath in most cases. Among other things it allows easily selecting nested nodes and avoids having to "check for null" each level as may be required with 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);
  • Hello and thank you, but now the program doesn't see the element I need. I mean that if (myEgitimBilgileri != null) is null. – Abdulyar Aug 27 '11 at 09:31
  • Yes, I wanted to use Elemets(). Can you help to run this code? – Abdulyar Aug 27 '11 at 09:41
  • @Abdulyar Verify the namespace is correct and they are correctly specified in the XML. There are two namespaces mentioned in the post. (I suspect that the XML fragment shown is *not* the Root Element of the document...? `Element/Elements` only looks at direct children. Consider `XPathSelectElements` for more advanced queries.) –  Aug 27 '11 at 09:44
  • If you don't mind,can have a talk with you in online messenger? – Abdulyar Aug 27 '11 at 09:51
  • @Abdulyar Prefer not, sorry. Feel free up update the post with any relevant information (full XML, for instance) and I can update my answer from there. If that is indeed the root element, it looks like there is a `xmlns:my=....` missing on it. –  Aug 27 '11 at 10:04
  • Ok, is not root. The root is . I cheked it, and it works, but how can call the if it is not root? – Abdulyar Aug 27 '11 at 10:12
  • @Abdulyar I updated my answer with an XPath example. More examples and documetation can be found online. –  Aug 27 '11 at 10:28
  • Sorry for my foolishness, but the program doesn't show the result in console. How we can write it to a new file? – Abdulyar Aug 27 '11 at 10:33
  • @Abdulyar I have answered the questions in the post. Consider adding a new question with the new problem/issues so that they can be addressed. –  Aug 27 '11 at 10:37