如何解析存储在字符串中的Xml代码? [英] How Can I Parse This Xml Code Stored In Strings?

查看:74
本文介绍了如何解析存储在字符串中的Xml代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i有两个这样的字符串:



string scope =

<? xml version = \   1.0 \encoding = \UTF-8 \?> + 
< haspscope> +
< hasp type = \\ \\HASP-HL \/> +
< / haspscope> ;;





string format =

 <   haspformat     root   =   \  hasp_info\ > + 
< 功能 > +
< 属性 < span class =code-attribute> name = \ id \ / > +
< 属性 名称 = \ locked\ / > +
< attribute name = \ expired \ / > +
< attribute 名称 = \ disabled \ / > +
< 属性 名称 < span class =code-keyword> = \ useful\ < span class =code-attribute> / > +
< / feature > +
< / haspformat > ;





被调用的方法接收这两个字符串并返回第三个字符串,如下所示:



字符串结果=

 <? xml version = < span class =code-string>  1.0 encoding =   UTF-8 ?>  
< <跨度class =code-leadattribute> hasp_info >
< < span class =code-leadattribute> feature id = 0 已锁定 = true 已过期 = false 已禁用 = false 可用 = true / >
< feature id = 1 已锁定 < span class =code-keyword> = true 已过期 = false 已禁用 = false 可用 = true / >
< / hasp_info >





如何获取第二个功能ID的值(它是1 )?



谢谢

解决方案

所有XML解析器都允许解析存储在字符串中的XML,通过 MemoryString (如果没有直接实现从字符串解析)。这是我对.NET FCL中可用解析器的简短概述:



  1. 使用 System.Xml.XmlDocument class。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。
  2. 使用类 System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。
  3. 使用类 System.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。





要从流中读取字符串内容,请使用 System.IO.MemoryStream

http://msdn.microsoft.com/en-us/library/system .io.memorystream%28v = vs.110%29.aspx [ ^ ]。



放置字符串的代码示例内存流显示在上面引用的MSDN文章中。您只需要在读取之前将流回滚到第一个位置: memStream.Seek(0,SeekOrigin.Begin); 。从这一点开始,您可以从此流中读取XML。



-SA


一种可能方法是从字符串创建System.Xml.XmlDocument对象,然后找到必要的节点

  var  xml =  new  System.Xml.XmlDocument(){InnerXml = result}; 
var nodes = xml.GetElementsByTagName( 特征);
var node = nodes.Cast< XmlElement>()。SingleOrDefault(e = > e.GetAttribute( id)== 1);
if (node!= null
{
// 做某事,例如get node.Attributes
}


选项#1使用XmlDocument ...

  string  xml =  <? xml version = \1.0 \encoding = \UTF-8 \?>< hasp_info>< feature id = \0 \locked = \true \已过期= \false \disabled = \false \useful = \true \/>< feature id = \1 \locked = \true \ expired = \false \disabled = \false \useful = \true \/>< / hasp_info>; 
System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
xdoc.LoadXml(xml);
System.Xml.XmlNode node = xdoc.SelectSingleNode( // hasp_info / feature [@id =' 1' ]);

// 对所选节点执行某些操作



选项#2使用LINQ to XML ...

  string  xml =  <?xml version = \1.0 \encoding = \UTF-8 \ ?>< hasp_info>< feature id = \0 \locked = \true \expired = \false \disabled = \false \可用= \\ \\true \/>< feature id = \1 \locked = \true \expired = \false \disabled = \false \useful = \true \/>< / hasp_info>; 
System.Xml.Linq.XElement doc = System.Xml.Linq.XElement.Parse(xml);
System.Xml.Linq.XElement element =( from el in doc.Elements()
其中 el.Attribute( id )。值== 0
选择 el).FirstOrDefault< System.Xml.Linq.XElement>();
// 对所选元素执行某些操作


hi,
i have two strings like this:

string scope =

"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + 
"<haspscope>" + 
"    <hasp type=\"HASP-HL\" />" + 
"</haspscope>";



string format =

"<haspformat root=\"hasp_info\">" + 
"    <feature>" + 
"       <attribute name=\"id\" />" + 
"       <attribute name=\"locked\" />" + 
"       <attribute name=\"expired\" />" + 
"       <attribute name=\"disabled\" />" + 
"       <attribute name=\"usable\" />" + 
"    </feature>" + 
"</haspformat>";



the called method receives these two string and returns an third string like this:

string result=

<?xml version="1.0" encoding="UTF-8" ?>
<hasp_info>
  <feature id="0" locked="true" expired="false" disabled="false" usable="true" />
  <feature id="1" locked="true" expired="false" disabled="false" usable="true" />
</hasp_info>



How can i get the value of the second feature id (it's 1) ?

Thanks

解决方案

All XML parsers allow to parse XML stored in a string, via a MemoryString (if parsing from string is not directly implemented). This is my short overview of those parsers available in .NET FCL:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].



For reading a string content from stream, use System.IO.MemoryStream:
http://msdn.microsoft.com/en-us/library/system.io.memorystream%28v=vs.110%29.aspx[^].

The code sample with putting a string to a memory stream is shown in the MSDN article referenced above. You only need to rewind the stream back to the first position before reading: memStream.Seek(0, SeekOrigin.Begin);. From this point, you can read XML from this stream.

—SA


one possible approach is to create System.Xml.XmlDocument object from string and then find the necessary node

var xml = new System.Xml.XmlDocument() {InnerXml = result};
var nodes = xml.GetElementsByTagName("feature");
var node = nodes.Cast<XmlElement>().SingleOrDefault(e => e.GetAttribute("id") == "1");
if (node !=null)
{
   // do something, e.g. get node.Attributes
}


Option #1 using XmlDocument...

string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><hasp_info><feature id=\"0\" locked=\"true\" expired=\"false\" disabled=\"false\" usable=\"true\" /><feature id=\"1\" locked=\"true\" expired=\"false\" disabled=\"false\" usable=\"true\" /></hasp_info>";
System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
xdoc.LoadXml(xml);
System.Xml.XmlNode node = xdoc.SelectSingleNode("//hasp_info/feature[@id = '1']");

// do something with the selected node


Option #2 using LINQ to XML...

string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><hasp_info><feature id=\"0\" locked=\"true\" expired=\"false\" disabled=\"false\" usable=\"true\" /><feature id=\"1\" locked=\"true\" expired=\"false\" disabled=\"false\" usable=\"true\" /></hasp_info>";
System.Xml.Linq.XElement doc = System.Xml.Linq.XElement.Parse(xml);
System.Xml.Linq.XElement element = (from el in doc.Elements()
                                                where el.Attribute("id").Value == "0"
                                                select el).FirstOrDefault<System.Xml.Linq.XElement>();
// do something with the selected element


这篇关于如何解析存储在字符串中的Xml代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆