哪位大哥能发个范例告诉我怎么用c#来操作xml吗?

javanow 2002-03-13 05:27:25
经过我2天时间的查资料,勉强知道怎么读xml内容,怎么append内容。
但我想随便在xml指定节点来插入一个节点或删除一个节点我该怎么做呢?
多谢了
我的mail: shenzhenbird@hotmail.com
100分送了,不够还可以加的
...全文
92 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
javanow 2002-03-13
  • 打赏
  • 举报
回复
我现在自己会insert了,是看“xml 3.0技术内幕”
可惜是为c/c++写的
谢谢大家的帮助。

真理原来在自己手上,所以jhnhu也有分.呵呵

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;


namespace xmlNow
{
/// <summary>
/// Summary description for Insert.
/// </summary>
public class Insert : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{

XmlDocument doc = new XmlDocument();
XmlTextReader reader = new XmlTextReader("c:\\books.xml");
doc.Load(reader);
reader.Close();

XmlNode root = doc.DocumentElement;
/*
XmlNode newNode = doc.CreateNode( XmlNodeType.Element,"NewNode","New Node Value");
// get <NewNode xmlns="New Node Value" />
*/

XmlElement message ;
XmlElement authorInfo;

XmlNode newNode = doc.CreateNode( XmlNodeType.Element , "book","");


message = doc.CreateElement("title");
message.InnerText="雨季不再来";
newNode.AppendChild(message);



authorInfo = doc.CreateElement("author");
newNode.AppendChild(authorInfo);



message = doc.CreateElement("first-name");
message.InnerText="陈";
authorInfo.AppendChild(message);

message = doc.CreateElement("last-name");
message.InnerText="三毛";
authorInfo.AppendChild(message);


XmlNode currNode = root.InsertBefore(newNode,root.ChildNodes.Item(1));

XmlTextWriter writer = new XmlTextWriter("c:\\books.xml",null);
writer.Formatting=Formatting.Indented;
doc.Save(writer);
reader.Close();

writer.Close();















}
}
}
javanow 2002-03-13
  • 打赏
  • 举报
回复
error occured
javanow 2002-03-13
  • 打赏
  • 举报
回复
/sigh jhnhu(jhnhu)
javanow (大家都不理我了) 看着jhnhu(jhnhu),深深地叹了口气。
/faint jhnhu(jhnhu)
javanow (大家都不理我了)被 jhnhu(jhnhu) 气得昏了过去。

谢谢 goldish()
我看完再结吧。
看看哪位大哥能再给我发一个来。
goldish 2002-03-13
  • 打赏
  • 举报
回复
发了,你收一下邮件,希望对你有用。
jhnhu 2002-03-13
  • 打赏
  • 举报
回复
再花两天时间不就能知道了吗!

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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