Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读取xml文件值的c#代码_C#_Xml_Linq_Xml Parsing_Xmlreader - Fatal编程技术网

读取xml文件值的c#代码

读取xml文件值的c#代码,c#,xml,linq,xml-parsing,xmlreader,C#,Xml,Linq,Xml Parsing,Xmlreader,我有这个xml文件,我想提取作者姓名和访问号码,我在C#中有一个非常简单的实现,我在C#中使用xml阅读器并逐行阅读。但我正在寻找一种实现,在这种实现中,我可以高效地读取c#中的作者姓名和访问号码。我是C#新手,有人告诉我应该使用LINQ,但查看文档和此文件,我无法讲述如何使用Xdocument。任何帮助都将不胜感激 <xml> <records> <record> <database name="CP_EndnoteLibrary_2012-2015-

我有这个xml文件,我想提取作者姓名和访问号码,我在C#中有一个非常简单的实现,我在C#中使用xml阅读器并逐行阅读。但我正在寻找一种实现,在这种实现中,我可以高效地读取c#中的作者姓名和访问号码。我是C#新手,有人告诉我应该使用LINQ,但查看文档和此文件,我无法讲述如何使用Xdocument。任何帮助都将不胜感激

<xml>
<records>
<record>
<database name="CP_EndnoteLibrary_2012-2015-1.enl" path="C:\Users\Downloads\file.enl">file.enl</database>
<source-app name="EndNote" version="17.4">EndNote</source-app>
<rec-number>24</rec-number>
<contributors>
<authors>
<author>
<style face="normal" font="default" size="100%">ABCD, X.</style>
</author>
<author>
<style face="normal" font="default" size="100%">EFGH, I.</style>
</author> 
</authors>
</contributors>
<accession-num>
<style face="normal" font="default" size="100%">12345678</style>
</accession-num>
</record>
<record>...</record>
</records>
}

上面的代码似乎效率很低,我正在寻找改进的方法,或者以更好的方式进行

    //Helpfull namespaces:
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Serialization;

        static void Main(string[] args)
    {
        //Your snippet, which didn't work on my machine:
        XmlReader reader = XmlReader.Create("C:\\Users\\Public\\ile_xml.xml");
        while (reader.Read())
        {
            if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "author"))
            {
                reader.Read();
                reader.Read();
                if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "style") && reader.HasAttributes)
                {
                    var val = reader.ReadInnerXml();
                    Console.WriteLine("Display:" + reader.GetAttribute("author"));
                }
            }
        }
        //Should produce the results you are looking for:
        XmlNodeList xmlNodeList;
        XmlDocument xDoc = new XmlDocument();
        XmlReaderSettings xrs = new XmlReaderSettings();
        xrs.DtdProcessing = DtdProcessing.Parse;

        //Get Authors from XML Source
        using (XmlReader reader2 = XmlReader.Create("C:\\Users\\Public\\ile_xml.xml"))
        {
            xDoc.Load(reader2);
            xmlNodeList = xDoc.SelectNodes("records/record/contributors/authors/author");
        }
        foreach (XmlNode node in xmlNodeList)
        {
            Console.WriteLine(node.InnerText);//.InnerXML to include style tags.
        };
    }
xpath将帮助您找到所需的信息。希望以上内容能让您更接近xdoc

我最近采用的另一种模式是将xml序列化为c#类(或者在本例中是一个列表),然后使用LINQ根据需要进行操作

这对我很有帮助:

xpath将帮助您找到所需的信息。希望以上内容能让您更接近xdoc

我最近采用的另一种模式是将xml序列化为c#类(或者在本例中是一个列表),然后使用LINQ根据需要进行操作


这对我很有帮助:

这将为您提供正确的结果:-

XDocument xdoc = XDocument.Load(@"YourXMLfilePath");
var result = xdoc.Root.Elements("record")
                      .Select(x => new
                                 {
                                    Name = (string)x.Element("database").Attribute("name"),
                                    Number = (string)x.Element("rec-number")
                                 });

这将为您提供正确的结果:-

XDocument xdoc = XDocument.Load(@"YourXMLfilePath");
var result = xdoc.Root.Elements("record")
                      .Select(x => new
                                 {
                                    Name = (string)x.Element("database").Attribute("name"),
                                    Number = (string)x.Element("rec-number")
                                 });

我有使用xcode的示例代码,但我不确定如何为输入xml文件实现它。我将尝试使用这个作为示例,看看我是否能找到解决方案。谢谢我有使用xcode的示例代码,但我不确定如何为输入xml文件实现它。我将尝试使用这个作为示例,看看我是否能找到解决方案。谢谢@yguw-你有机会检查我的答案吗?@yguw-你有机会检查我的答案吗?