C# 如何使用LINQtoXML读取XML?
我的XML文件没有重复的信息(例如,提要XML文件)。我只需要从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 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
这是我的评论
从上述文件中,我只需要以下元素的值:-
- 名字
- 数
- 头衔
- 评论
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的成员。