C#中能否用XmlWriter修改一个已经存在的xml文件?(添加一个特定节点)
我的xml文件是用.net里的DataSet.WriteXml方法直接生成的,因为这个xml需要在网络上传送,现在需要添加一个节点(Base64编码的图片),可能得用xm...
我的xml文件是用.net里的DataSet.WriteXml方法直接生成的,因为这个xml需要在网络上传送,现在需要添加一个节点(Base64编码的图片),可能得用xmlWriter写,能不能对一个已存在的xml文件用XmlWriter来在特定位置添加一个节点?
:imouse728 可是你并没有用XmlWriter啊 还是用的DOM不是吗, 而且我要写入二进制文件的编码. 展开
:imouse728 可是你并没有用XmlWriter啊 还是用的DOM不是吗, 而且我要写入二进制文件的编码. 展开
2个回答
展开全部
可以的
WriterXml.aspx文件内容:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WriterXml.aspx.cs" Inherits="WriterXml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
WriterXml.aspx.cs文件内容
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class WriterXml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
WriteXmlFile("adfile.xml");
}
}
private void WriteXmlFile(string file)
{
XmlDocument xmldoc = new XmlDocument();
try
{ ///导入xml文档
xmldoc.Load(Server.MapPath(file));
XmlNode root = xmldoc.SelectSingleNode("Advertisements");
if(root != null)
{ ///创建<Ad>结点
XmlNode node = xmldoc.CreateNode(XmlNodeType.Element,"Ad",null);
///创建5个孩子结点
CreateNode(xmldoc,node,"ImageUrl","google.gif");
CreateNode(xmldoc,node,"NavigateUrl","http://www.google.com");
CreateNode(xmldoc,node,"AlternateText","Google搜索");
CreateNode(xmldoc,node,"Impressions","30");
CreateNode(xmldoc,node,"Keyword","google");
///创建<Ad>结点
root.AppendChild(node);
}
///保xml存文档
xmldoc.Save(Server.MapPath(file));
Response.Write("写入XML文件adfile.xml成功。" + "<br>");
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
/// <summary>
/// 创建一个XmlNode
/// </summary>
/// <param name="xmldoc"></param>
/// <param name="parentnode"></param>
/// <param name="name"></param>
/// <param name="value"></param>
private void CreateNode(XmlDocument xmldoc,XmlNode parentnode,string name,
string value)
{ ///创建XmlNode,并设置InnerText属性
XmlNode node = xmldoc.CreateNode(XmlNodeType.Element,name,null);
node.InnerText = value;
parentnode.AppendChild(node);
}
}
adfile.xml内容:
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<ImageUrl>gucas.gif</ImageUrl>
<NavigateUrl>http://www.gucas.ac.cn</NavigateUrl>
<AlternateText>中国科学院研究生院</AlternateText>
<Impressions>60</Impressions>
<Keyword>gucas</Keyword>
</Ad>
<Ad>
<ImageUrl>sina.gif</ImageUrl>
<NavigateUrl>http://www.sina.com.cn</NavigateUrl>
<AlternateText>新浪网站</AlternateText>
<Impressions>30</Impressions>
<Keyword>sina</Keyword>
</Ad>
<Ad>
<ImageUrl>baidu.gif</ImageUrl>
<NavigateUrl>http://www.baidu.com</NavigateUrl>
<AlternateText>百度搜索</AlternateText>
<Impressions>30</Impressions>
<Keyword>baidu</Keyword>
</Ad>
<Ad>
<ImageUrl>google.gif</ImageUrl>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>Google搜索</AlternateText>
<Impressions>30</Impressions>
<Keyword>google</Keyword>
</Ad>
<Ad>
<ImageUrl>google.gif</ImageUrl>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>Google搜索</AlternateText>
<Impressions>30</Impressions>
<Keyword>google</Keyword>
</Ad>
</Advertisements>
WriterXml.aspx文件内容:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WriterXml.aspx.cs" Inherits="WriterXml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
WriterXml.aspx.cs文件内容
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class WriterXml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
WriteXmlFile("adfile.xml");
}
}
private void WriteXmlFile(string file)
{
XmlDocument xmldoc = new XmlDocument();
try
{ ///导入xml文档
xmldoc.Load(Server.MapPath(file));
XmlNode root = xmldoc.SelectSingleNode("Advertisements");
if(root != null)
{ ///创建<Ad>结点
XmlNode node = xmldoc.CreateNode(XmlNodeType.Element,"Ad",null);
///创建5个孩子结点
CreateNode(xmldoc,node,"ImageUrl","google.gif");
CreateNode(xmldoc,node,"NavigateUrl","http://www.google.com");
CreateNode(xmldoc,node,"AlternateText","Google搜索");
CreateNode(xmldoc,node,"Impressions","30");
CreateNode(xmldoc,node,"Keyword","google");
///创建<Ad>结点
root.AppendChild(node);
}
///保xml存文档
xmldoc.Save(Server.MapPath(file));
Response.Write("写入XML文件adfile.xml成功。" + "<br>");
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
/// <summary>
/// 创建一个XmlNode
/// </summary>
/// <param name="xmldoc"></param>
/// <param name="parentnode"></param>
/// <param name="name"></param>
/// <param name="value"></param>
private void CreateNode(XmlDocument xmldoc,XmlNode parentnode,string name,
string value)
{ ///创建XmlNode,并设置InnerText属性
XmlNode node = xmldoc.CreateNode(XmlNodeType.Element,name,null);
node.InnerText = value;
parentnode.AppendChild(node);
}
}
adfile.xml内容:
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<ImageUrl>gucas.gif</ImageUrl>
<NavigateUrl>http://www.gucas.ac.cn</NavigateUrl>
<AlternateText>中国科学院研究生院</AlternateText>
<Impressions>60</Impressions>
<Keyword>gucas</Keyword>
</Ad>
<Ad>
<ImageUrl>sina.gif</ImageUrl>
<NavigateUrl>http://www.sina.com.cn</NavigateUrl>
<AlternateText>新浪网站</AlternateText>
<Impressions>30</Impressions>
<Keyword>sina</Keyword>
</Ad>
<Ad>
<ImageUrl>baidu.gif</ImageUrl>
<NavigateUrl>http://www.baidu.com</NavigateUrl>
<AlternateText>百度搜索</AlternateText>
<Impressions>30</Impressions>
<Keyword>baidu</Keyword>
</Ad>
<Ad>
<ImageUrl>google.gif</ImageUrl>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>Google搜索</AlternateText>
<Impressions>30</Impressions>
<Keyword>google</Keyword>
</Ad>
<Ad>
<ImageUrl>google.gif</ImageUrl>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>Google搜索</AlternateText>
<Impressions>30</Impressions>
<Keyword>google</Keyword>
</Ad>
</Advertisements>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询