Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am trying to read directory from xml file in c# and have problem

Tags:

c#

xml

<?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 xml containing directory of pdf file which I would need to read. I can read first name and last name from _Page_1 node but do not 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();

Thank you BrokenGlass, it's working. I have one more question. What if I have several iteration of _Document_Definition node, how do 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>
like image 751
developer Avatar asked Nov 29 '25 03:11

developer


1 Answers

You are missing the XML namespace references to access those 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;
like image 84
BrokenGlass Avatar answered Nov 30 '25 18:11

BrokenGlass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!