日期:2014-05-19  浏览次数:21165 次

c# 操作xml文档
如题!

------解决方案--------------------
//creat
try
{
if (!(System.IO.File.Exists(System.IO.Directory.GetCurrentDirectory() + @ "temp.xml ")))
{
XmlTextWriter xw = new XmlTextWriter(System.IO.Directory.GetCurrentDirectory() + @ "temp.xml ", Encoding.GetEncoding( "GB2312 "));

xw.WriteStartDocument();
xw.WriteStartElement( "bookstore ");
xw.WriteStartElement( "book ");

xw.WriteAttributeString( "name ", "文化苦旅 ");
xw.WriteAttributeString( "id ", "001 ");
xw.WriteElementString( "author ", "余秋雨 ");
xw.WriteElementString( "price ", "32 ");
xw.WriteElementString( "press ", "华夏出版社 ");
xw.WriteEndElement();

xw.WriteStartElement( "book ");
xw.WriteAttributeString( "name ", "三国演义 ");
xw.WriteAttributeString( "id ", "002 ");
xw.WriteElementString( "author ", "罗贯中 ");
xw.WriteElementString( "price ", "45 ");
xw.WriteElementString( "press ", "青年出版社 ");
xw.WriteEndElement();

xw.WriteStartElement( "book ");
xw.WriteAttributeString( "name ", "人性的弱点 ");
xw.WriteAttributeString( "id ", "003 ");
xw.WriteElementString( "author ", "卡耐基 ");
xw.WriteElementString( "price ", "30 ");
xw.WriteElementString( "press ", "清华出版社 ");
xw.WriteEndElement();

xw.WriteEndElement();
xw.WriteEndDocument();
xw.Flush();
xw.Close();
}
}
//get value
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(System.IO.Directory.GetCurrentDirectory() + @ "temp.xml ");
XmlNode xn = xmldoc.SelectSingleNode( "/bookstore/book[@name = '三国演义 ']/press ");
MessageBox.Show(xn.InnerText);
}
catch
{
}
------解决方案--------------------
给分吧
------解决方案--------------------
简单问题。baidu,google,csdn都有很多例子的。
------解决方案--------------------
你的xml内容有问题,那些标签里面有错误,你可以将xml拿到VS2005看一下。会提示那里有问题的。
------解决方案--------------------
给吧!