简体   繁体   English

我试图从c#中的xml文件中读取目录并且有问题

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

<?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. 我有xml包含pdf文件的目录,我需要阅读。 I can read first name and last name from _Page_1 node but do not know how to read ImagePath. 我可以从_Page_1节点读取名字和姓氏,但不知道如何阅读ImagePath。 Here is my code to read from _Page_1 这是我从_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. 谢谢BrokenGlass,它正在工作。 I have one more question. 我还有一个问题。 What if I have several iteration of _Document_Definition node, how do I read each iteration. 如果我有几个_Document_Definition节点的迭代怎么办,我怎么读每次迭代。

<?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>

You are missing the XML namespace references to access those attributes, this works: 您缺少用于访问这些属性的XML命名空间引用,这有效:

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;

It looks like Imagepath is an attribute not an element. 看起来Imagepath是一个属性而不是一个元素。 Hence you are not able to read it. 因此,您无法阅读它。 Check for the attributes in the xml file. 检查xml文件中的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用 c# 从 xml 文件中读取 url - I am trying to read a url from an xml file using c# using below 试图从XML文件读取的C#XML阅读器 - C# XML reader trying to read from a XML file C# 我试图将一个文件从一个目录移动到一个新目录,在那里我不知道文件的名称是什么 - C# I am trying to move a file from one directory, where I wont know what the name of the file is, to a new directory 如何从C#的项目目录中读取Xml文件? - How Read Xml File from Project Directory in C#? 我正在尝试从 xml 文件中读取正则表达式,但是当我将正则表达式传递给 C# 代码时,却得到了错误匹配 - I'm trying to read a Regex from an xml file but when i pass on the Regex to the C# code but I'm getting false matches 我正在尝试从C#中的自定义文件类型读取 - I'm trying to read from a custom file type in C# 我正在尝试在.NET环境中使用C#解析XML文件,并且该文件不断跳过元素 - I am trying to parse an XML file using C# in the .NET environment and it keeps skipping over elements 我正在尝试编写的C#函数的奇怪问题 - Strange problem with a C# function I am trying to write 我在尝试将 CSV 文件读入列表时遇到错误<int> C#</int> - I am getting an error trying to read a CSV file in to a List<int> c# 为什么在尝试从XML文件读取数据时出现Null Referenfe异常? - Why am I getting Null Referenfe exception while trying to read data from XML file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM