如何访问属性值,这些属性值现在是xml字符串格式。 [英] How to access attribute values, which are now in an xml string format..
本文介绍了如何访问属性值,这些属性值现在是xml字符串格式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨..
有一个表,其中一列是xml字符串的形式,这个字符串包含另外5个列值。其中xml字符串就像是子表的第一个..我想访问此xml字符串中的属性值以检查搜索条件。请告诉我如何访问属性..请给我一个示例代码来实现...
提前致谢。
swathi。
解决方案
这是我可以使用的不同XML解析方法的简短概述:
- 使用
System.Xml.XmlDocument
类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。- 使用类
System.Xml.XmlTextReader
;这是最快的阅读方式,特别是你需要跳过一些数据。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。- 使用类
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 [ ^ ]。
-SA
string str = 你的xml字符串;
XDocument document = XDocument.Parse(str);
var books = document.Descendants( 书跨度>);
foreach (XElement item 书籍)
{
var idElement = item.Element( id );
if (idElement.HasAttributes)
{
XAttribute attr = idElement.Attribute( isPrimaryKey);
string attrValue = attr.Value;
Console.WriteLine(attrValue);
}
}
Console.Read();
XML:
<? xml version = 1.0 encoding = utf-8 ?>
< 图书 >
< book >
< id < span class =code-attribute> isPrimaryKey = true > 3785 < / id >
< 名称 > CLR通过C# < / name >
< / book >
< / books > 跨度>
Hi..
There is a table in which one column is in the form of an xml string,and this string contain another 5 column values.here the xml string is like child table of the first one..i want to access the attribute values in this xml string for checking a search criteria..Tell me please how can i access attributes..Please give me an example code to implement that..
Thanks in advance.
swathi.
解决方案
This is my short overview of different XML parsing methods you can use:
- 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[^].- 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[^].- Use the class
System.Xml.Linq.XDocument
; this is the most adequate way similar to that ofXmlDocument
, 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[^].
—SA
string str = "your xml string"; XDocument document = XDocument.Parse(str); var books = document.Descendants("book"); foreach (XElement item in books) { var idElement = item.Element("id"); if (idElement.HasAttributes) { XAttribute attr = idElement.Attribute("isPrimaryKey"); string attrValue = attr.Value; Console.WriteLine(attrValue); } } Console.Read();
XML:
<?xml version="1.0" encoding="utf-8"?> <books> <book> <id isPrimaryKey="true">3785</id> <name>CLR via C#</name> </book> </books>
这篇关于如何访问属性值,这些属性值现在是xml字符串格式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文