一个很简单问题~!!!!

FrostG 2003-09-30 09:43:49
在ASP.NET当中.我怎么把用户输入的信息写进XML文件里呢~!?我知道有这样一个类.
请问是哪个类...怎么用...谢谢了~!!!
...全文
30 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qimini 2003-09-30
  • 打赏
  • 举报
回复
昏,點'管理'
FrostG 2003-09-30
  • 打赏
  • 举报
回复
喔...这样啊...谢谢咯~!!!
对了...怎么给分数啊~!?
zhenwang 2003-09-30
  • 打赏
  • 举报
回复
给你一个简洁的步骤:
1、创建一个XMLDocument对象,用于载入存储信息的XML文件;
XmlDocument xdoc=new XmlDocument();
xdoc.Load(Server.MapPath("XML文件名.xml"));

2、创建一个新的主节点,并将它添加到跟节点下:
XmlElement parentnode=xdoc.CreateElement("MainNode");
xdoc.DocumentElement.PrependChild(parentnode);

3、创建所有用于存储信息的子节点:
XmlElement node1=xdoc.CreateElement("node1");
XmlElement node2=xdoc.CreateElement("node2");
......................

4、将上面创建的各个存储信息的节点添加到主节点下但并不包含最终的值:
parentnode.AppendChild(node1);
parentnode.AppendChild(node2);
...........................

5、将要添加的文本信息添加到各个节点中去:
node1.AppendChild("----文本信息---");
node2.AppendChild("----文本信息---");

6、保存存储信息的XML文件
xdoc.Save(Server.MapPath("xml文件名.xml"));

按照上面的方法创建的 xml文件将具有如下格式:
<?xml version="1.0" encoding="gb2312"?>
<mainnode>
<node1>....</node1>
<node2>.....</node2>
</mainnode>
qimini 2003-09-30
  • 打赏
  • 举报
回复
====================A sample From MSDN 2003

Input

data1|data2|data3|data4|data5

and you would like to transform it to :

Output

<data>
<item> data1 </item>
<item> data2 </item>
<item> data3 </item>
<item> data4 </item>
<item> data5 </item>
</data>

A method is created to tokenize the data in the file into individual data tokens. Each call to this function returns one of the data elements. The method takes in a StreamReader.


[C#]
public static String NextToken(StreamReader stream)
{
int temp = stream.Read();
String t1 = "";
while(temp != -1 && temp != (int)'|')
{
t1 += (char)temp;
temp = stream.Read();
}
return t1;
}

The code iterates through until there are no data tokens remaining; inserting each token into an XML document. This is done in the Main function, which takes two command line arguments. The first argument is the file to read from, the second is the file to write to. The comments within the code provide a description of the actions taking place.

[C#]
using System;
using System.IO;
using System.Xml;

namespace TextFiletoXml
{

class TextFiletoXml
{
public static String file;
public static StreamReader stream;
public static XmlTextWriter xwriter;
public static String val;
public static String NextToken(StreamReader stream)
{
int temp = stream.Read();
String t1 = "";
while(temp != -1 && temp != (int)'|')
{
t1 += (char)temp;
temp = stream.Read();
}

return t1;

}

public static void Main(string[] args)
{
file = args[0];

//Create a new stream representing the file we are
//reading from.
stream = new StreamReader(file, true);

//Create a new XmlTextWriter.
xwriter = new XmlTextWriter(args[1],System.Text.Encoding.UTF8);
//Write the beginning of the document including the
//document declaration. Standalone is true.

xwriter.WriteStartDocument(true);

//Write the beginning of the "data" element. This is
//the opening tag to our data

xwriter.WriteStartElement("data","www.alpineskihouse.com");

// Get the first data element from the file.
val = NextToken(stream);

//create a new element with each data token from //the stream.
while(val != "")
{

xwriter.WriteElementString("item","www.alpineskihouse.com", val);
val = NextToken(stream);
}
//End the "data" element.
xwriter.WriteEndElement();

//End the document
xwriter.WriteEndDocument();

//Flush the xml document to the underlying stream and
//close the underlying stream. The data will not be
//written out to the stream until either the Flush()
//method is called or the Close() method is called.
xwriter.Close();
}
}
}
qimini 2003-09-30
  • 打赏
  • 举报
回复
==================== From MSDN 2003

Input

data1|data2|data3|data4|data5

and you would like to transform it to :

Output

<data>
<item> data1 </item>
<item> data2 </item>
<item> data3 </item>
<item> data4 </item>
<item> data5 </item>
</data>

A method is created to tokenize the data in the file into individual data tokens. Each call to this function returns one of the data elements. The method takes in a StreamReader.


[C#]
public static String NextToken(StreamReader stream)
{
int temp = stream.Read();
String t1 = "";
while(temp != -1 && temp != (int)'|')
{
t1 += (char)temp;
temp = stream.Read();
}
return t1;
}

The code iterates through until there are no data tokens remaining; inserting each token into an XML document. This is done in the Main function, which takes two command line arguments. The first argument is the file to read from, the second is the file to write to. The comments within the code provide a description of the actions taking place.

[C#]
using System;
using System.IO;
using System.Xml;

namespace TextFiletoXml
{

class TextFiletoXml
{
public static String file;
public static StreamReader stream;
public static XmlTextWriter xwriter;
public static String val;
public static String NextToken(StreamReader stream)
{
int temp = stream.Read();
String t1 = "";
while(temp != -1 && temp != (int)'|')
{
t1 += (char)temp;
temp = stream.Read();
}

return t1;

}

public static void Main(string[] args)
{
file = args[0];

//Create a new stream representing the file we are
//reading from.
stream = new StreamReader(file, true);

//Create a new XmlTextWriter.
xwriter = new XmlTextWriter(args[1],System.Text.Encoding.UTF8);
//Write the beginning of the document including the
//document declaration. Standalone is true.

xwriter.WriteStartDocument(true);

//Write the beginning of the "data" element. This is
//the opening tag to our data

xwriter.WriteStartElement("data","www.alpineskihouse.com");

// Get the first data element from the file.
val = NextToken(stream);

//create a new element with each data token from //the stream.
while(val != "")
{

xwriter.WriteElementString("item","www.alpineskihouse.com", val);
val = NextToken(stream);
}
//End the "data" element.
xwriter.WriteEndElement();

//End the document
xwriter.WriteEndDocument();

//Flush the xml document to the underlying stream and
//close the underlying stream. The data will not be
//written out to the stream until either the Flush()
//method is called or the Close() method is called.
xwriter.Close();
}
}
}
qimini 2003-09-30
  • 打赏
  • 举报
回复
XmlWriter

62,040

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧