简体   繁体   English

如何在C#中读取XML In

[英]How to Read XML In in C#

It may sounds a very basic question, but here it is. 这听起来可能是一个非常基本的问题,但事实确实如此。 I have this sample XML loaded in DOM 我已将此示例XML加载到DOM中

<message from="fromvalue" to="tovalue" xml:lang="en" type="chat">
   <thread>{fe422b47-9856-4404-8c35-5ff45e43ee01}</thread> 
   <body>Test buddy</body> 
   <active xmlns="http://XXXX.org/protocol/chatstates" /> 
 </message>

This i am receiving from request body using the following piece of code 这是我使用以下代码从请求正文中收到的

StreamReader reader = new StreamReader ( HttpContext.Request.InputStream,        System.Text.Encoding.UTF8 );
string sXMLRequest = reader.ReadToEnd ( );
XmlDocument xmlRequest = new XmlDocument ( );
xmlRequest.LoadXml ( sXMLRequest );

Now all i need to have is value of three things in three different variable 现在我需要拥有的是三个不同变量中的三件事的值

string bodytext = {body element inner text}
string msgfrom = {from attribute value of message element}
string msgto =   {to attribute value of message element}

I am using C#, can anyone put some minutes from their precious time and guide me, will appreciate that highly 我正在使用C#,任何人都可以从宝贵的时间中抽出一些时间来指导我,我会非常感激

I'd use LINQ to XML here - it's much simpler: 我将在这里使用LINQ to XML-这简单得多:

XDocument doc = XDocument.Load(HttpContext.Request.InputStream);
string bodyText = (string) doc.Root.Element("body");
string fromAddress = (string) doc.Root.Attribute("from");
string toAddress = (string) doc.Root.Attribute("to");

That will give you a value of null for any element/attribute which isn't present. 对于任何不存在的元素/属性,这将为您提供null值。 If you're happy with a NullReferenceException instead: 如果您对NullReferenceException感到满意,请执行以下操作:

XDocument doc = XDocument.Load(HttpContext.Request.InputStream);
string bodyText = doc.Root.Element("body").Value;
string fromAddress = doc.Root.Attribute("from").Value;
string toAddress = doc.Root.Attribute("to").Value;

You could use XDocument which is the new XML parser that was introduced in .NET 3.5: 您可以使用XDocument ,它是.NET 3.5中引入的新XML解析器:

XDocument doc = XDocument.Parse(sXMLRequest);
string bodytext = doc.Element("message").Element("body").Value;
string msgfrom = doc.Element("message").Attribute("from").Value;
string msgto = doc.Element("message").Attribute("to").Value;

I much prefer XLINQ, however in your examples case: 我更喜欢XLINQ,但是在您的示例情况下:

XmlNode thread_node = xmlRequest.SelectSingleNode("/message/thread");
Guid thread = thread_node.InnerText;

XmlNode body_node = xmlRequest.SelectSingleNode("/message/body");
string body= body_node.InnerText;

Etc... 等等...

It's quite easy to serialize / deserialize between Xml and a C# class: 在Xml和C#类之间进行序列化/反序列化非常容易:

http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization

You basically create a class that holds the elements and attributes of the Xml, stick [XmlElement] and [XmlAttribute] attributes on them, and use the XmlSerializer . 您基本上可以创建一个类,其中包含Xml的元素和属性,将[XmlElement]和[XmlAttribute]属性粘贴在它们上,然后使用XmlSerializer

There are other options; 还有其他选择。 the XmlReader you already mentioned requires alot of work / maintenance, and is generally only used for large documents. 您已经提到的XmlReader需要大量工作/维护,并且通常仅用于大型文档。 The other answers I've seen sofar use readers of a higher abstraction level that are easy to use and do not use such a proxy class. 我见过的其他答案使用的是较高抽象级别的读者,这些读者易于使用,并且不使用此类代理类。 I guess it's a matter of preference. 我想这是偏爱的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM