c#代码读取xml文件值

时间:2022-10-26 17:02:17

I have this xml file and I want to extract author name and access number and what I have is a very naive implementation in C# where I am using xml reader and reading line by line. But I am looking for an implementation where I can read the author name and access number in c# efficiently. I am new to C# and I have been told that LINQ should be used but looking at the document and this file I am not able to relate how to use Xdocument. Any help will be appreciated.

我有这个xml文件,我想提取作者姓名和访问号码,我在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>

Following a document, I was able to write this code to figure out author name.

在文档之后,我能够编写此代码来找出作者姓名。

{
    class Program
    {
        static void Main(string[] args)
        {
        XmlReader reader = XmlReader.Create("C:\\Users\\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"));
                }
            }
        }
    }
}

}

The above code seems to be very inefficient and I am looking for ways to improve this or do it in a better way.

上面的代码似乎非常低效,我正在寻找改进方法或以更好的方式做到这一点的方法。

2 个解决方案

#1


0  

This will give you the correct result:-

这将给你正确的结果: -

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")
                                 });

#2


0  

    //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 will help find the information you need. Hopefully the above will get you closer with xdoc.

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

Another pattern I have recently adopted is to serialize the xml into c# class (or in this case a List) and then use LINQ to manipulate as desired.

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

this was helpful to me: Deserializing XML to Objects in C#

这对我很有帮助:在C#中将XML反序列化为对象

#1


0  

This will give you the correct result:-

这将给你正确的结果: -

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")
                                 });

#2


0  

    //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 will help find the information you need. Hopefully the above will get you closer with xdoc.

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

Another pattern I have recently adopted is to serialize the xml into c# class (or in this case a List) and then use LINQ to manipulate as desired.

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

this was helpful to me: Deserializing XML to Objects in C#

这对我很有帮助:在C#中将XML反序列化为对象