C# XML 文件操作.docx
- 文档编号:9537567
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:20
- 大小:20.75KB
C# XML 文件操作.docx
《C# XML 文件操作.docx》由会员分享,可在线阅读,更多相关《C# XML 文件操作.docx(20页珍藏版)》请在冰豆网上搜索。
C#XML文件操作
C#读写XML
(一)
publicvoidwriter()
{
XmlTextWriterxtw=newXmlTextWriter(Server.MapPath("~/test1.xml"),System.Text.Encoding.UTF8);
xtw.WriteStartDocument();
xtw.WriteStartElement("Root");
for(inti=0;i<3;i++)
{
xtw.WriteStartElement("img");
xtw.WriteAttributeString("width","400");
xtw.WriteAttributeString("height","266");
xtw.WriteEndElement();
}
xtw.WriteEndElement();
xtw.WriteEndDocument();
xtw.Close();
}
System.Xml.XmlDocumentdom=newSystem.Xml.XmlDocument();
dom.Load("test.xml");//转载xml
System.Xml.XmlNodeListxnl=dom.GetElementsByTagName("sheng");//获取所有sheng节点
foreach(System.Xml.XmlNodenodeinxnl)
{
List
foreach(System.Xml.XmlNodenode2innode.ChildNodes)//查找所有dian节点
{
if(node2.Name=="dian")
list.Add(node2);
}
foreach(System.Xml.XmlNodenode2inlist)//删除节点
{
node.RemoveChild(node2);
}
}
dom.Save("out.xml");
下表给出了这些方法的名字和描述情况:
方法描述:
WriteStartDocument书写版本为“1.0”的XML声明
WriteEndDocument关闭任何打开的元素或属性
Close关闭流
WriteDocType写出具有指定名称和可选属性的DOCTYPE声明
WriteStartElement写出指定的开始标记
WriteEndElement关闭一个元素
WriteFullEndElement关闭一个元素,并且总是写入完整的结束标记
WriteElementString写出包含字符串值的元素
WriteStartAttribute书写属性的起始内容
WriteEndAttribute关闭上一个WriteStartAttribute调用
WriteRaw手动书写原始标记
WriteString书写一个字符串
WriteAttributeString出具有指定值的属性
WriteCData写出包含指定文本的
[CDATA[...]]>块
WriteComment写出包含指定文本的注释
--...-->
WriteWhiteSpace写出给定的空白
WriteProcessingInstruction写出在名称和文本之间带有空格的处理指令,如下所示:
nametext?
>如果你对于XML十分熟悉,那么你一定能很好的理解上面的这些方法。
下面我们将给出一个例子,在这个例子中,我们将先创建一个文档,添加一些元素,然后关闭这个文档。
添加了元素后你还可以添加子元素,属性和其他内容。
下面的代码就是这样的一个例子,它创建了一个文件名为title的XML文件。
usingSystem;usingSystem.IO;
usingSystem.Xml;
publicclassSample
{
publicstaticvoidMain()
{
XmlTextWriterwriter=newXmlTextWriter("titles.xml",null);
//写入根元素
writer.WriteStartElement("items");
//加入子元素
writer.WriteElementString("title","UnrealTournament2003");
writer.WriteElementString("title","C&C:
Renegade");
writer.WriteElementString("title","Dr.Seuss'sABC");
//关闭根元素,并书写结束标签
writer.WriteEndElement();
//将XML写入文件并且关闭
XmlTextWriterwriter.Close();
}
}
如果你编译并且执行上面的代码,你将创建这个XML文件,文件中包含如下内容:
Renegade
上面的代码创建了一个名为writer的XmlTextWriter对象。
当这个对象被创建时,它被关联到一个名为titles.xml的文件。
接着,程序创建了一个叫做items的根属性,WriteStartElement方法创建了这个属性的开始标签。
接下来,程序调用了WriteElementString方法创建了三个子元素。
从上面的代码你还可以看到,这个方法使用第一个参数(在上面的程序中是title)作为元素的标签;使用第二个参数作为元素的值。
当你添加了所有的元素后,你需要关闭根元素。
这时你可以调用WriteEndElement方法关闭那个最近被打开的元素;在本例中,这个最近被打开的元素就是根元素。
当所有的数据都已经写好,根元素也已经关闭时,你可以将信息传送给你的XmlTextWriter。
这意味着这时候你可以调用Close方法关闭它了。
上面的代码相对十分的简单,下面我们看一个使用了XmlTextWriter类中更多方法,功能更加完善的例子。
usingSystem;usingSystem.IO;
usingSystem.Xml;
publicclassSample
{
publicstaticvoidMain()
{
XmlTextWriterwriter=newXmlTextWriter("myMedia.xml",null);
//使用自动缩进便于阅读
writer.Formatting=Formatting.Indented;
//书写根元素
writer.WriteStartElement("items");
//开始一个元素
writer.WriteStartElement("item");
//向先前创建的元素中添加一个属性
writer.WriteAttributeString("rating","R");
//添加子元素
writer.WriteElementString("title","TheMatrix");
writer.WriteElementString("format","DVD");
//关闭item元素
writer.WriteEndElement();
//关闭元素
//在节点间添加一些空格
writer.WriteWhitespace("n");
//使用原始字符串书写第二个元素
writer.WriteRaw("
//使用格式化的字符串书写第三个元素
writer.WriteRaw("n
//关闭根元素
writer.WriteFullEndElement();
//将XML写入文件并关闭
writerwriter.Close();}
}
上面代码编译运行后将得到myMedia.xml文件,文件的内容为:
写的一个XML操作类,包括读取/插入/修改/删除。
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Xml;
namespacePuTianCheng
{
///
///XmlHelper的摘要说明
///
publicclassXmlHelper
{
publicXmlHelper()
{
}
///
///读取数据
///
///
///
///
///
/**************************************************
*使用示列:
*XmlHelper.Read(path,"/Node","")
*XmlHelper.Read(path,"/Node/Element[@Attribute='Name']","Attribute")
************************************************/
publicstaticstringRead(stringpath,stringnode,stringattribute)
{
stringvalue="";
try
{
XmlDocumentdoc=newXmlDocument();
doc.Load(path);
XmlNodexn=doc.SelectSingleNode(node);
value=(attribute.Equals("")?
xn.InnerText:
xn.Attributes[attribute].Value);
}
catch{}
returnvalue;
}
///
///插入数据
///
///
///
///
///
///
///
/**************************************************
*使用示列:
*XmlHelper.Insert(path,"/Node","Element","","Value")
*XmlHelper.Insert(path,"/Node","Element","Attribute","Value")
*XmlHelper.Insert(path,"/Node","","Attribute","Value")
************************************************/
publicstaticvoidInsert(stringpath,stringnode,stringelement,stringattribute,stringvalue)
{
try
{
XmlDocumentdoc=newXmlDocument();
doc.Load(path);
XmlNodexn=doc.SelectSingleNode(node);
if(element.Equals(""))
{
if(!
attribute.Equals(""))
{
XmlElementxe=(XmlElement)xn;
xe.SetAttribute(attribute,value);
}
}
else
{
XmlElementxe=doc.CreateElement(element);
if(attribute.Equals(""))
xe.InnerText=value;
else
xe.SetAttribute(attribute,value);
xn.AppendChild(xe);
}
doc.Save(path);
}
catch{}
}
///
///修改数据
///
///
///
///
///
///
/**************************************************
*使用示列:
*XmlHelper.Insert(path,"/Node","","Value")
*XmlHelper.Insert(path,"/Node","Attribute","Value")
************************************************/
publicstaticvoidUpdate(stringpath,stringnode,stringattribute,stringvalue)
{
try
{
XmlDocumentdoc=newXmlDocument();
doc.Load(path);
XmlNodexn=doc.SelectSingleNode(node);
XmlElementxe=(XmlElement)xn;
if(attribute.Equals(""))
xe.InnerText=value;
else
xe.SetAttribute(attribute,value);
doc.Save(path);
}
catch{}
}
///
///删除数据
///
///
///
///
///
///
/**************************************************
*使用示列:
*XmlHelper.Delete(path,"/Node","")
*XmlHelper.Delete(path,"/Node","Attribute")
************************************************/
publicstaticvoidDelete(stringpath,stringnode,stringattribute)
{
try
{
XmlDocumentdoc=newXmlDocument();
doc.Load(path);
XmlNodexn=doc.SelectSingleNode(node);
XmlElementxe=(XmlElement)xn;
if(attribute.Equals(""))
xn.ParentNode.RemoveChild(xn);
else
xe.RemoveAttribute(attribute);
doc.Save(path);
}
catch{}
}
}
}
==================================================
XmlFile.xml:
xmlversion="1.0"encoding="utf-8"?
>
==================================================
使用方法:
stringxml=Server.MapPath("XmlFile.xml");
//插入元素
//XmlHelper.Insert(xml,"/Root","Studio","","");
//插入元素/属性
//XmlHelper.Insert(xml,"/Root/Studio","Site","Name","小路工作室");
//XmlHelper.Insert(xml,"/Root/Studio","Site","Name","丁香鱼工作室");
//XmlHelper.Insert(xml,"/Root/Studio","Site","Name","谱天城工作室");
//XmlHelper.Insert(xml,"/Root/S
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# XML 文件操作 文件 操作