如何读取xml文件以及如何将读取的值存储在asp.net中的字符串变量中 [英] how to read a xml file and also how to store the read value in string variable in asp.net
本文介绍了如何读取xml文件以及如何将读取的值存储在asp.net中的字符串变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨!我想读取xml文件,并将读取的值也存储在asp.net中的字符串变量中.
Hi!!i want to read a xml file and also store the read value in string variable in asp.net
推荐答案
让我从结尾开始:通过将值分配给变量来完成在字符串变量中存储值".如果您不理解它,我不确定谈论XML是否有意义. :-)
无论如何,至少有三种方法可以读取标准.NET库支持的XML.这是我对它们的简短评论:
Let me start with the end: "store… value in string variable" is something done by assigning a value to a variable. If you did not understand it, I am not sure if it make any sense to talk about XML or not. :-)
Anyway, there are at least three ways to read XML supported by standard .NET libraries. Here is my short review of them:
- 使用
System.Xml.XmlDocument
类.它实现了DOM接口;如果文档太大,则这种方法最简单,也足够好.
请参见- 使用类
].System.Xml.XmlTextReader
; library/system.xml.xmldocument.aspx"target =" _ blank"title =" New Window> ^ - 使用类
- 使用类
System.Xml.XmlTextReader
;这是最快的读取方法,尤其是您需要跳过一些数据.
请参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [http://msdn.microsoft.com/en-us/library/bb387063.aspx [
- 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[^].
string value1;
string value2;
protected void Page_Load(object sender, EventArgs e)
{
Read_Xmlfile();
}
public void Read_Xmlfile()
{
XmlTextReader reader = new XmlTextReader("D:\\WebSite2008\\WebSite3Tierdemo2\\file\\myxml.xml");
while (reader.Read())
{
XmlNodeType nodeType = reader.NodeType;
switch (nodeType)
{
case XmlNodeType.Element:
if (reader.HasAttributes)
{
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
//value2=reader.Value;
//Response.Write("Attribute is {0} with Value {1}: ", reader.Name, reader.Value);
}
}
break;
case XmlNodeType.Text:
//Response.Write(reader.Value);
value1 = reader.Value;
Response.Write(value1);
break;
}
}
//Label1.Text = value1;
//Label2.Text = value2;
}
也可以执行以下操作:
Also you may do the following :
String path = Server.MapPath("~\\App_Data\\Ooohh.xml");
if (File.Exists(path))
{
DataSet ds = new DataSet();
ds.ReadXml(path);
GridView1.DataSource = ds;
GridView1.DataBind();
ds.Dispose();
}
在这种情况下,XML被读取为表格并显示在GridView1中.不确定将读取的值存储在字符串变量中"是什么意思.
In this case the XML is read as a table and shown in the GridView1. Not sure what you mean by "store the read value in string variable".
这篇关于如何读取xml文件以及如何将读取的值存储在asp.net中的字符串变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文