急!!!如何通过XmlReader,XmlWriter实现添加内容的功能 如题,请问,如何使用XmlReader,XmlWriter实现以下功能:读取XML文件,将读取器指向某一位置,然后在该位置开始写入新文本。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 XmlReader ,XmlWriter是没有办法实现的.可以用XmlDocument类来实现 应该先找到要插入的位置,然后插入。即可。XmlElement newxe = xmlDoc.CreateElement("Dic");newxe.SetAttribute("name", word);插入。即可。 搞错了, 以为是xmltextreader,writer.. XMLWiter只能顺序推进的写xml文件,不能定位到文件中的某个位置添加元素或节点,要定位并添加节点或元素用XmlDocument吧 XmlDocument xd = new XmlDocument(); xd.Load(@"C:\test\aa.xml"); //创建新的元素 XmlElement dd = xd.CreateElement("DD"); dd.InnerText = "text"; //添加到原来的xml xd.DocumentElement.AppendChild(dd); //保存 xd.Save(@"C:\test\aa.xml"); 有几种技术1,XmlDocument(DOM)操作2,XmlReader,XmlWriter,3,Linq to XML,//这三种技术,1相对来说我比较熟悉一点,2也可以,但是麻烦一点,3,还不太会要是你要用第2种技术的话,那请先参见:http://msdn2.microsoft.com/zh-cn/library/aa302289.aspx 如果LZ你一定要xmlreader和xmlwriter进行操作的话可以看看楼上贴的连接. 蛮好的 找到节点位置,然后开始ADD不就行了 顶8楼的用xml类来操作节点 我自己写的,用来配置应用程序数据库信息的。你自己看看整理吧。不想整理了。using System.IO;using System.Xml;/// <summary> /// 应用程序配置项的基类 /// </summary> public abstract class XMLConfig { protected string errordes; /// <summary> /// 最后的错误描述 /// </summary> public string ErrorDes { get { return errordes; } } // 针对 XML 的操作 #region /// <summary> /// 在指定结点 保存 Name 项的值为 value /// </summary> /// <param name="nd">指定结点</param> /// <param name="Name">项名称</param> /// <param name="Value">项的值</param> protected void Write(XmlNode nd, string Name, string Value) { XmlNode xe = null; bool bfound = Search(nd, Name, ref xe); if (bfound) { xe.InnerText = Value; } else { XmlElement xenew = nd.OwnerDocument.CreateElement(Name); xenew.InnerText = Value; nd.AppendChild(xenew); } } /// <summary> /// 加载 在实现中从指定结点 nd 中读取指定项 /// </summary> /// <param name="nd">指定结点</param> /// <param name="Name">项名称</param> /// <param name="Value">项的值</param> /// <returns>true:读取成功; false:读取失败</returns> protected bool Read(XmlNode nd ,string Name, ref string Value) { XmlNode xe = null ; if (!Search(nd, Name, ref xe)) return false; Value = xe.InnerText; return true; } /// <summary> /// 查找指定结点的子结点中是否含指定的子结点 /// </summary> /// <param name="xnl">目标结点</param> /// <param name="Name">查找的子结点</param> /// <param name="xe">找到的子结点</param> /// <returns>True:有子结点;false:没有</returns> protected bool Search(XmlNode nd, string Name, ref XmlNode xe) { foreach (XmlNode xn in nd.ChildNodes) { if (xn.Name == Name) { xe = xn; return true; } } return false; } #endregion /// <summary> /// 配置项的根结点 /// </summary> public XmlNode RootNode; /// <summary> /// 我的结点名称 /// 在根结点下面创建该结点 ,子类的全部配置项存储在该结点下面 /// </summary> public string MyNodeName; /// <summary> /// 重置类或结构的全部数据为默认值 /// </summary> abstract public void ResetDefaultValue(); } /// <summary> /// 数据库配置类 /// </summary> public class DataBaseConfig : XMLConfig { public string DataSource; public string UserID; public string PassWord; public string DataBase; public DataBaseConfig() { this.MyNodeName = "DataBaseConfig"; this.ResetDefaultValue(); } /// <summary> /// 返回链接字符串 /// </summary> /// <returns></returns> public override string ToString() { return "Data Source=" + DataSource + ";UID=" + UserID + " ;pwd=" + PassWord + ";database=" + DataBase + ";"; } /// <summary> /// 返回 oracle 的连接字符串 /// </summary> /// <returns></returns> public string ToOraString() { return this.ToString(); } /// <summary> /// 重置全部变量为默认值 /// </summary> public override void ResetDefaultValue() { this.DataSource = "127.0.0.1"; this.UserID = "sa"; this.PassWord = "sa"; this.DataBase = "pubs"; } public void Save() { base.Write(this.RootNode, this.MyNodeName, ""); XmlNode xe = null; base.Search(this.RootNode, this.MyNodeName, ref xe); base.Write(xe, "DataSource", this.DataSource); base.Write(xe, "UserID", this.UserID); base.Write(xe, "PassWord", this.PassWord); base.Write(xe, "DataBase", this.DataBase); } public bool Load() { bool bret = true; XmlNode xe = null; if (!this.Search(this.RootNode, this.MyNodeName, ref xe)) { this.errordes = "没有根结点:" + this.MyNodeName; return false; } if (!base.Read(xe, "DataSource", ref this.DataSource)) bret = false; if (!base.Read(xe, "UserID", ref this.UserID)) bret = false; if (!base.Read(xe, "PassWord", ref this.PassWord)) bret = false; if (!base.Read(xe, "DataBase", ref this.DataBase)) bret = false; this.DataSource = this.DataSource.Trim(); this.UserID = this.UserID.Trim(); this.PassWord = this.PassWord.Trim(); this.DataBase = this.DataBase.Trim(); return bret; } }/// </summary> /// <returns></returns> public bool Load() { bool bret = true; try { if (!File.Exists(ConfigureFile)) { errordes = "配置文件:" + ConfigureFile + " 不存在"; this.ResetDefaultValue(); return false; } XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(ConfigureFile); base.RootNode = xmldoc.SelectSingleNode("AppConfig"); localSQLServer.RootNode = base.RootNode; if ( ! localSQLServer.Load() ) { bret = false; this.errordes += localSQLServer.ErrorDes; }} /// <summary> /// 保存配置信息 /// </summary> public void Save() { try { if (!File.Exists(ConfigureFile)) { XmlTextWriter writer = new XmlTextWriter(ConfigureFile, Encoding.Unicode); writer.WriteStartDocument(true); writer.WriteComment("创建于:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); writer.WriteStartElement("AppConfig"); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); } XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(ConfigureFile); base.RootNode = xmldoc.SelectSingleNode("AppConfig"); this.localSQLServer.RootNode = base.RootNode; this.localSQLServer.Save();} 忘了说了,最后两个函数:Load 和 save 是怎么使用 楼上的你段代码能行吗?我怎么看到,你那个ref的XmlNode没有被实例化啊??要实例化它的话需要XmlDocument.CreateElement(?..);吧 回 17 楼:ref 目的是让 Search 函数返回找到的结点,其实用 Out 意思更明了。这个肯定是行的,现在就在使用着呢,主要是从基类比较容易扩展其它的变量,我感觉。大家共同研究。:-) 大家说的都不错,可是大家没有想过用XPathNavigator吗,我觉得综合性能,功能来讲,XPathNavigator是最好用的。 关于ManagementObjectSearcher 帮帮忙。。很急。。 gridview 关于datagridview 列类型的问题 懂javascript的帮个忙 我看极少人能遇到的 有什么小软件可以替代vs2005在服务器端运行asp.net程序呢? 请问VS 2005能不能通过配置更换Framework的? 怎么把一段文字复制到 richtextbox 里,(代码怎么些) 求一些函数? 哪里有wpf的教程能下载呀? asp.net中如何将table的内容导出到execl表中
XmlElement newxe = xmlDoc.CreateElement("Dic");
newxe.SetAttribute("name", word);插入。即可。
xd.Load(@"C:\test\aa.xml");
//创建新的元素
XmlElement dd = xd.CreateElement("DD");
dd.InnerText = "text";
//添加到原来的xml
xd.DocumentElement.AppendChild(dd);
//保存
xd.Save(@"C:\test\aa.xml");
1,XmlDocument(DOM)操作
2,XmlReader,XmlWriter,
3,Linq to XML,//
这三种技术,1相对来说我比较熟悉一点,2也可以,但是麻烦一点,3,还不太会要是你要用第2种技术的话,那请先参见:
http://msdn2.microsoft.com/zh-cn/library/aa302289.aspx
用xml类来操作节点
using System.IO;
using System.Xml;/// <summary>
/// 应用程序配置项的基类
/// </summary>
public abstract class XMLConfig
{
protected string errordes;
/// <summary>
/// 最后的错误描述
/// </summary>
public string ErrorDes
{
get { return errordes; }
} // 针对 XML 的操作
#region
/// <summary>
/// 在指定结点 保存 Name 项的值为 value
/// </summary>
/// <param name="nd">指定结点</param>
/// <param name="Name">项名称</param>
/// <param name="Value">项的值</param>
protected void Write(XmlNode nd, string Name, string Value)
{
XmlNode xe = null;
bool bfound = Search(nd, Name, ref xe);
if (bfound)
{
xe.InnerText = Value;
}
else
{
XmlElement xenew = nd.OwnerDocument.CreateElement(Name);
xenew.InnerText = Value;
nd.AppendChild(xenew);
}
}
/// <summary>
/// 加载 在实现中从指定结点 nd 中读取指定项
/// </summary>
/// <param name="nd">指定结点</param>
/// <param name="Name">项名称</param>
/// <param name="Value">项的值</param>
/// <returns>true:读取成功; false:读取失败</returns>
protected bool Read(XmlNode nd ,string Name, ref string Value)
{
XmlNode xe = null ;
if (!Search(nd, Name, ref xe)) return false;
Value = xe.InnerText;
return true;
}
/// <summary>
/// 查找指定结点的子结点中是否含指定的子结点
/// </summary>
/// <param name="xnl">目标结点</param>
/// <param name="Name">查找的子结点</param>
/// <param name="xe">找到的子结点</param>
/// <returns>True:有子结点;false:没有</returns>
protected bool Search(XmlNode nd, string Name, ref XmlNode xe)
{
foreach (XmlNode xn in nd.ChildNodes)
{
if (xn.Name == Name)
{
xe = xn;
return true;
}
}
return false;
}
#endregion /// <summary>
/// 配置项的根结点
/// </summary>
public XmlNode RootNode;
/// <summary>
/// 我的结点名称
/// 在根结点下面创建该结点 ,子类的全部配置项存储在该结点下面
/// </summary>
public string MyNodeName;
/// <summary>
/// 重置类或结构的全部数据为默认值
/// </summary>
abstract public void ResetDefaultValue();
}
/// <summary>
/// 数据库配置类
/// </summary>
public class DataBaseConfig : XMLConfig
{
public string DataSource;
public string UserID;
public string PassWord;
public string DataBase; public DataBaseConfig()
{
this.MyNodeName = "DataBaseConfig";
this.ResetDefaultValue();
}
/// <summary>
/// 返回链接字符串
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "Data Source=" + DataSource + ";UID=" + UserID + " ;pwd=" + PassWord + ";database=" + DataBase + ";";
}
/// <summary>
/// 返回 oracle 的连接字符串
/// </summary>
/// <returns></returns>
public string ToOraString()
{
return this.ToString();
}
/// <summary>
/// 重置全部变量为默认值
/// </summary>
public override void ResetDefaultValue()
{
this.DataSource = "127.0.0.1";
this.UserID = "sa";
this.PassWord = "sa";
this.DataBase = "pubs";
}
public void Save()
{
base.Write(this.RootNode, this.MyNodeName, "");
XmlNode xe = null;
base.Search(this.RootNode, this.MyNodeName, ref xe);
base.Write(xe, "DataSource", this.DataSource);
base.Write(xe, "UserID", this.UserID);
base.Write(xe, "PassWord", this.PassWord);
base.Write(xe, "DataBase", this.DataBase);
}
public bool Load()
{
bool bret = true;
XmlNode xe = null;
if (!this.Search(this.RootNode, this.MyNodeName, ref xe))
{
this.errordes = "没有根结点:" + this.MyNodeName;
return false;
}
if (!base.Read(xe, "DataSource", ref this.DataSource)) bret = false;
if (!base.Read(xe, "UserID", ref this.UserID)) bret = false;
if (!base.Read(xe, "PassWord", ref this.PassWord)) bret = false;
if (!base.Read(xe, "DataBase", ref this.DataBase)) bret = false; this.DataSource = this.DataSource.Trim();
this.UserID = this.UserID.Trim();
this.PassWord = this.PassWord.Trim();
this.DataBase = this.DataBase.Trim();
return bret;
}
}/// </summary>
/// <returns></returns>
public bool Load()
{
bool bret = true;
try
{
if (!File.Exists(ConfigureFile))
{
errordes = "配置文件:" + ConfigureFile + " 不存在";
this.ResetDefaultValue();
return false;
} XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(ConfigureFile);
base.RootNode = xmldoc.SelectSingleNode("AppConfig");
localSQLServer.RootNode = base.RootNode;
if ( ! localSQLServer.Load() )
{
bret = false;
this.errordes += localSQLServer.ErrorDes;
}
} /// <summary>
/// 保存配置信息
/// </summary>
public void Save()
{
try
{
if (!File.Exists(ConfigureFile))
{
XmlTextWriter writer = new XmlTextWriter(ConfigureFile, Encoding.Unicode);
writer.WriteStartDocument(true);
writer.WriteComment("创建于:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
writer.WriteStartElement("AppConfig");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(ConfigureFile);
base.RootNode = xmldoc.SelectSingleNode("AppConfig"); this.localSQLServer.RootNode = base.RootNode;
this.localSQLServer.Save();
}
我怎么看到,你那个ref的XmlNode没有被实例化啊??要实例化它的话需要XmlDocument.CreateElement(?..);吧
ref 目的是让 Search 函数返回找到的结点,其实用 Out 意思更明了。
这个肯定是行的,现在就在使用着呢,主要是从基类比较容易扩展其它的变量,我感觉。
大家共同研究。
:-)