I have an XML file which looks like this
我有一个像这样的XML文件
<ss:demo>
<ss:Name>
<ss:FirstName>First</ss:FirstName>
<ss:SecondName>Second</ss:SecondName>
</ss:Name>
<ss:country code="IN">India</ss:country>
</ss:demo>
How can I read this using C#. Please help me in this. which will be easiest way for reading it? I tried to read it into a DataSet but its showing some error.
如何使用c#读取这个。请在这件事上帮助我。阅读它的最简单的方法是什么?我试图把它读进数据集中,但它显示出一些错误。
2 个解决方案
#1
6
There are several strategies to read an XML document, or parts thereof, using C#. If you are more specific about what you want to do with the XML document, the community can provide you with more specific guidance.
使用c#读取XML文档或XML文档的部分有几种策略。如果您对要使用XML文档做什么更明确,社区可以为您提供更具体的指导。
Here are some top choices:
以下是一些最好的选择:
Linq to XML
Linq to XML
http://msdn.microsoft.com/en-us/library/bb387098.aspx
http://msdn.microsoft.com/en-us/library/bb387098.aspx
http://msdn.microsoft.com/en-us/library/bb387065.aspx
http://msdn.microsoft.com/en-us/library/bb387065.aspx
XDocument (part of the Linq to XML framework)
XDocument (Linq到XML框架的一部分)
http://msdn.microsoft.com/en-us/library/bb387063.aspx
http://msdn.microsoft.com/en-us/library/bb387063.aspx
XmlDocument
XmlDocument
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
XPath
XPath
http://msdn.microsoft.com/en-us/library/ms256471.aspx
http://msdn.microsoft.com/en-us/library/ms256471.aspx
#2
3
How about just LINQ to XML?
只需要LINQ to XML怎么样?
Given this:
鉴于这种:
var xml = "<ss:demo>\r\n<ss:Name>\r\n <ss:FirstName>First</ss:FirstName>\r\n <ss:SecondName>" +
"Second</ss:SecondName>\r\n</ss:Name>\r\n<ss:country code=\"IN\">India</ss:country>\r\n</ss" +
":demo>";
(Note I had to wrap IN
in quotes as such "IN"
)
(注意,我必须用“IN”这样的引号括起来)
Declare some namespaces and read it in:
声明一些名称空间并将其读入:
var mngr = new XmlNamespaceManager( new NameTable() );
mngr.AddNamespace( "ss", "urn:ignore" ); // or proper URL
var parserContext = new XmlParserContext(null, mngr, null, XmlSpace.None, null);
If reading from a string as above:
如果从上面的字符串中读取:
var txtReader = new XmlTextReader( xml, XmlNodeType.Element, parserContext );
If reading from a file instead:
如果从文件中读取,则改为:
var txtReader = new XmlTextReader( new FileStream(filename, FileMode.Open), XmlNodeType.Element, parserContext );
Then load:
然后加载:
var ele = XElement.Load( txtReader );
ele
contains everything you need.
爱乐包含了你所需要的一切。
#1
6
There are several strategies to read an XML document, or parts thereof, using C#. If you are more specific about what you want to do with the XML document, the community can provide you with more specific guidance.
使用c#读取XML文档或XML文档的部分有几种策略。如果您对要使用XML文档做什么更明确,社区可以为您提供更具体的指导。
Here are some top choices:
以下是一些最好的选择:
Linq to XML
Linq to XML
http://msdn.microsoft.com/en-us/library/bb387098.aspx
http://msdn.microsoft.com/en-us/library/bb387098.aspx
http://msdn.microsoft.com/en-us/library/bb387065.aspx
http://msdn.microsoft.com/en-us/library/bb387065.aspx
XDocument (part of the Linq to XML framework)
XDocument (Linq到XML框架的一部分)
http://msdn.microsoft.com/en-us/library/bb387063.aspx
http://msdn.microsoft.com/en-us/library/bb387063.aspx
XmlDocument
XmlDocument
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
XPath
XPath
http://msdn.microsoft.com/en-us/library/ms256471.aspx
http://msdn.microsoft.com/en-us/library/ms256471.aspx
#2
3
How about just LINQ to XML?
只需要LINQ to XML怎么样?
Given this:
鉴于这种:
var xml = "<ss:demo>\r\n<ss:Name>\r\n <ss:FirstName>First</ss:FirstName>\r\n <ss:SecondName>" +
"Second</ss:SecondName>\r\n</ss:Name>\r\n<ss:country code=\"IN\">India</ss:country>\r\n</ss" +
":demo>";
(Note I had to wrap IN
in quotes as such "IN"
)
(注意,我必须用“IN”这样的引号括起来)
Declare some namespaces and read it in:
声明一些名称空间并将其读入:
var mngr = new XmlNamespaceManager( new NameTable() );
mngr.AddNamespace( "ss", "urn:ignore" ); // or proper URL
var parserContext = new XmlParserContext(null, mngr, null, XmlSpace.None, null);
If reading from a string as above:
如果从上面的字符串中读取:
var txtReader = new XmlTextReader( xml, XmlNodeType.Element, parserContext );
If reading from a file instead:
如果从文件中读取,则改为:
var txtReader = new XmlTextReader( new FileStream(filename, FileMode.Open), XmlNodeType.Element, parserContext );
Then load:
然后加载:
var ele = XElement.Load( txtReader );
ele
contains everything you need.
爱乐包含了你所需要的一切。