I am trying to read a directory from an xml file in C # and have problems

<?xml version="1.0" encoding="UTF-8"?>
<form:Documents xmlns:form="http://www.abbyy.com/FlexiCapture/Schemas/Export/FormData.xsd" xmlns:addData="http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd">
    <_Document_Definition_1:_Document_Definition_1 addData:ImagePath="C:\POC\Export\Test.pdf" xmlns:_Document_Definition_1="http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd">
        <_Page_1>
            <_First_Name>John</_First_Name>
            <_Last_Name>Doe</_Last_Name>
        </_Page_1>
    </_Document_Definition_1:_Document_Definition_1>
</form:Documents>

I have an xml containing a pdf file directory that I will need to read. I can read the first and last name from _Page_1 node, but I donโ€™t know how to read ImagePath. Here is my code to read from _Page_1

       XDocument xDoc = XDocument.Load("Test.xml");
       var poc = from p in xDoc.Descendants("_Page_1")
       select new
              {
                  FirstName = p.Element("_First_Name").Value,
                  LastNumber = p.Element("_Last_Name").Value
              };

        // Execute the query 
        foreach (var customer in poc)
        {
            Console.WriteLine(customer.FirstName);
            Console.WriteLine(customer.LastName);
        }

        //Pause the application 
        Console.ReadLine();

Thanks BrokenGlass, it works. I've got one more question. What if I have several iterations of the _Document_Definition node, how can I read each iteration.

<?xml version="1.0" encoding="UTF-8"?>
<form:Documents xmlns:form="http://www.abbyy.com/FlexiCapture/Schemas/Export/FormData.xsd" xmlns:addData="http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd">
    <_Document_Definition_1:_Document_Definition_1 addData:ImagePath="C:\POC\Export\Test.pdf" xmlns:_Document_Definition_1="http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd">
        <_Page_1>
            <_First_Name>John</_First_Name>
            <_Last_Name>Doe</_Last_Name>
        </_Page_1>
    </_Document_Definition_1:_Document_Definition_1>
<_Document_Definition_1:_Document_Definition_1 addData:ImagePath="C:\POC\Export\Test2.pdf" xmlns:_Document_Definition_1="http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd">
        <_Page_1>
            <_First_Name>Jane</_First_Name>
            <_Last_Name>Doe</_Last_Name>
        </_Page_1>
    </_Document_Definition_1:_Document_Definition_1>
</form:Documents>
+3
source share
2 answers

You do not have enough XML namespace to access these attributes, this works:

XDocument doc = XDocument.Load(@"test.xml");
XNamespace _Document_Definition_1 = "http://www.abbyy.com/FlexiCapture/Schemas/Export/Document_Definition_1.xsd";
XNamespace addData = "http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd";
string impagePath = doc.Descendants(_Document_Definition_1 + "_Document_Definition_1")
                       .First()
                       .Attribute(addData + "ImagePath")
                       .Value;
+1
source

Imagepath seems to be an attribute, not an element. Therefore, you cannot read it. Check the attributes in the XML file.

0
source

All Articles