Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.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
C# 如何使用LINQtoXML读取XML?_C#_Linq To Xml - Fatal编程技术网

C# 如何使用LINQtoXML读取XML?

C# 如何使用LINQtoXML读取XML?,c#,linq-to-xml,C#,Linq To Xml,我的XML文件没有重复的信息(例如,提要XML文件)。我只需要从xml文件中选择一些信息 <?xml version="1.0" encoding="UTF-8"?> <root> <client> <Name>abc, xyz's</Name> <DOB>2/1/1922</DOB> <Number>1234567896</Number&

我的XML文件没有重复的信息(例如,提要XML文件)。我只需要从xml文件中选择一些信息

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <client>
        <Name>abc, xyz's</Name>
        <DOB>2/1/1922</DOB>
        <Number>1234567896</Number>
        <Gender>unknown</Gender>
    </client>
    <Info>
        <ID>1111111111</ID>
        <Title>TITLE</Title>
    </Info>
    <BasicInfo>
        <TransDate>3/16/2011</TransDate>
        <Channel>1 + 1</Channel>
        <Ind></Ind>
        <Med></Med>
        <Comment>This is comment</Comment>
    </BasicInfo>
</root>

abc,xyz公司
2/1/1922
1234567896
未知的
1111111111
标题
3/16/2011
1 + 1
这是我的评论
从上述文件中,我只需要以下元素的值:-

  • 名字
    • 头衔
    • 评论
如何使用Linq to XML读取此文件?请帮助。

简单:

XDocument doc = XDocument.Load("feed.xml");

XElement client = doc.Root.Element("client");
string name = (string) client.Element("Name");
int number = (int) client.Element("Number");

XElement info = doc.Root.Element("Info");
string title = (string) info.Element("Title");

XElement basicInfo = doc.Root.Element("BasicInfo");
string comment = (string) basicInfo.Element("Comment");
这可以缩短,但为不同的元素提供单独的变量将使调试更容易。当然,上面的代码根本没有错误检查。。。根据您的情况,您可能需要加载或不加载:)

简单:

XDocument doc = XDocument.Load("feed.xml");

XElement client = doc.Root.Element("client");
string name = (string) client.Element("Name");
int number = (int) client.Element("Number");

XElement info = doc.Root.Element("Info");
string title = (string) info.Element("Title");

XElement basicInfo = doc.Root.Element("BasicInfo");
string comment = (string) basicInfo.Element("Comment");

这可以缩短,但为不同的元素提供单独的变量将使调试更容易。当然,上面的代码根本没有错误检查。。。根据您的情况,您可能需要加载或不加载:)

以防您(像我一样)被迫使用VB.Net:),下面是一个可能的解决方案:

Dim xdoc As XDocument = <?xml version="1.0" encoding="UTF-8"?>
                                    <root>
                                        <client>
                                            <Name>abc, xyz's</Name>
                                            <DOB>2/1/1922</DOB>
                                            <Number>1234567896</Number>
                                            <Gender>unknown</Gender>
                                        </client>
                                        <Info>
                                            <ID>1111111111</ID>
                                            <Title>TITLE</Title>
                                        </Info>
                                        <BasicInfo>
                                            <TransDate>3/16/2011</TransDate>
                                            <Channel>1 + 1</Channel>
                                            <Ind></Ind>
                                            <Med></Med>
                                            <Comment>This is comment</Comment>
                                        </BasicInfo>
                                    </root>


            Console.WriteLine(xdoc.<root>.<client>.<Name>.Value())
            Console.WriteLine("    {0}", xdoc.<root>.<client>.<Number>.Value())
            Console.WriteLine("    {0}", xdoc.<root>.<Info>.<Title>.Value())
            Console.WriteLine("    {0}", xdoc.<root>.<BasicInfo>.<Comment>.Value())
Dim xdoc As XDocument=
abc,xyz公司
2/1/1922
1234567896
未知的
1111111111
标题
3/16/2011
1 + 1
这是我的评论
Console.WriteLine(xdoc…Value())
Console.WriteLine(“{0}”,xdoc…Value())
Console.WriteLine(“{0}”,xdoc…Value())
Console.WriteLine(“{0}”,xdoc…Value())

为了防止您(像我一样)被迫使用VB.Net:),这里有一个可能的解决方案:

Dim xdoc As XDocument = <?xml version="1.0" encoding="UTF-8"?>
                                    <root>
                                        <client>
                                            <Name>abc, xyz's</Name>
                                            <DOB>2/1/1922</DOB>
                                            <Number>1234567896</Number>
                                            <Gender>unknown</Gender>
                                        </client>
                                        <Info>
                                            <ID>1111111111</ID>
                                            <Title>TITLE</Title>
                                        </Info>
                                        <BasicInfo>
                                            <TransDate>3/16/2011</TransDate>
                                            <Channel>1 + 1</Channel>
                                            <Ind></Ind>
                                            <Med></Med>
                                            <Comment>This is comment</Comment>
                                        </BasicInfo>
                                    </root>


            Console.WriteLine(xdoc.<root>.<client>.<Name>.Value())
            Console.WriteLine("    {0}", xdoc.<root>.<client>.<Number>.Value())
            Console.WriteLine("    {0}", xdoc.<root>.<Info>.<Title>.Value())
            Console.WriteLine("    {0}", xdoc.<root>.<BasicInfo>.<Comment>.Value())
Dim xdoc As XDocument=
abc,xyz公司
2/1/1922
1234567896
未知的
1111111111
标题
3/16/2011
1 + 1
这是我的评论
Console.WriteLine(xdoc…Value())
Console.WriteLine(“{0}”,xdoc…Value())
Console.WriteLine(“{0}”,xdoc…Value())
Console.WriteLine(“{0}”,xdoc…Value())

在这里使用XPath是一个不错的选择:

XDocument doc = XDocument.Load("file.xml");
string name = (string)doc.XPathSelectElement("//root/client/Name");
string title = (string)doc.XPathSelectElement("//root/Info/Title");
string comment = (string)doc.XPathSelectElement("//root/BasicInfo/Comment");

没有错误检查,但是如果您知道元素将在那里,那么这会很好地工作。

在这里使用XPath是一个不错的选择:

XDocument doc = XDocument.Load("file.xml");
string name = (string)doc.XPathSelectElement("//root/client/Name");
string title = (string)doc.XPathSelectElement("//root/Info/Title");
string comment = (string)doc.XPathSelectElement("//root/BasicInfo/Comment");

没有错误检查,但是如果您知道元素将在那里,那么这很有效。

@User13839404:是的。XElement、XDocument等是LINQ到XML API的成员。@User13839404:是的。XElement、XDocument等是LINQtoXMLAPI的成员。