首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

如何操作XML文件

2012-08-17 
怎么操作XML文件,求助using System.Xmlnamespace ModifyXmlDocument1{class ModifyXml{/* 实现在filename

怎么操作XML文件,求助
using System.Xml;
namespace ModifyXmlDocument1
{
  class ModifyXml
  {
  /* 实现在filename参数为文件名的xml文件中对所有price子元素值按discount打折,
  * 并保存修改的xml文件。*/
  static public void ModifyPrice(string filename, double discount)
  { /*怎么找到所有price子元素*/

}


  /* 在xml文件中删除Title是strTitle参数的Book元素,如果删除成功,保存修改的xml文件,
  * 并将修改的xml输出控制台,返回true,否则返回false,不输出。*/
  static public bool DeleteBook(string filename, string strTitle)
  {
  XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.Load(filename);
  XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;

  foreach (XmlNode xn in xnl)
  {
  XmlElement xe = (XmlElement)xn;
  if (xe.GetAttributeNode("title") == strTitle) /*这里应该怎么写:删除Title是strTitle参数的Book元素*/
  {
  xe.RemoveAttribute("title");//删除genre属性  
  }

  }
  xmlDoc.Save(filename);

  return true;
  }


  /*在xml文件中的bookstore元素最后添加以下book元素,并保存修改的xml文件:
  * <book ,genre="Computer" publicationdate="2008" >
  <title>C++ 程序设计</title>
  <author>
  <name>张文</name>
  </author>
  <price>39.50</price>
  </book>*/
  static public void InsertBook(string filename)
  {
  XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.Load(filename);
  XmlNode root = xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>  
  XmlElement xe1 = xmlDoc.CreateElement("book");//创建一个<book>节点  
  xe1.SetAttribute("genre", "Computer");//设置该节点genre属性  
  // xe1.SetAttribute("ISBN", "2-3631-4");//设置该节点ISBN属性  

  XmlElement xesub1 = xmlDoc.CreateElement("title");
  xesub1.InnerText = "C++ 程序设计";//设置文本节点  
  xe1.AppendChild(xesub1);//添加到<book>节点中  
  XmlElement xesub2 = xmlDoc.CreateElement("author");
  xesub2.InnerText = "张文";
  xe1.AppendChild(xesub2);
  XmlElement xesub3 = xmlDoc.CreateElement("price");
  xesub3.InnerText = "39.50";
  xe1.AppendChild(xesub3);

  root.AppendChild(xe1);//添加到<bookstore>节点中  
  xmlDoc.Save(filename);
  }


  /* 查询filename参数为文件名的xml中book/author/last-name值是lastName参数的Book元素,
  * 返回该book的子元素title值。*/
  static public void Main(string[] args)
  {
  InsertBook("C:\\books1.xml");
  }

  }
}
C:\\books1.xml文件:

XML code
<?xml version='1.0'?><!-- This file represents a fragment of a book store inventory database --><bookstore>  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author>   <first-name>Benjamin</first-name>   <last-name>Franklin</last-name> </author> <price>8.99</price>  </book>  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2"> <title>The Confidence Man</title> <author>   <first-name>Herman</first-name>   <last-name>Melville</last-name> </author> <price>11.99</price>  </book>  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author>   <name>Plato</name> </author> <price>9.99</price>  </book></bookstore> 



[解决办法]
XmlDocument doc=new XmlDocument();
doc.Load("");
foreach (XmlNode node in doc.GetElementsByTagName(""))
{
foreach (XmlNode xn in node.ChildNodes)
{
Console.WriteLine(xn.Attributes[""].Value);
}
}

XmlNode no= doc.SelectSingleNode("//bookstore/book [@Title='']");
if(no!=null)
{
XmlElement xe=(XmlElement)no;
xe.RemoveAll();
}
RemoveAll();//删除该节点的全部内容
XML操作
[解决办法]
1.
C# code
            XDocument doc=XDocument.Load("C:\\books1.xml");            List<string> PList = new List<string>();            var query = from price in doc.Descendants("price") select price.Value;            foreach (var item in query)             {                PList.Add(item);            }            doc.Save("C:\\books1.xml");
[解决办法]
C# code
    //你的filename对应xml哪个?我对应title做测试        public static void ModifyPrice(string filename, double discount)        {            XDocument Doc = XDocument.Load(@"C:\books1.xml");            var Query = from D in Doc.Element("bookstore").Elements("book")                        where D.Element("title").Value.IndexOf(filename) > -1                        select D;            foreach (var v in Query)            {                v.Element("price").Value = (Convert.ToDouble(v.Element("price").Value) * discount).ToString();            }            Doc.Save(@"C:\books1.xml");        }        public static bool DeleteBook(string strTitle)        {            bool Result = false;            try            {                XDocument Doc = XDocument.Load(@"C:\books1.xml");                Doc.Element("bookstore").Elements("book").Where(x => x.Element("title").Value == strTitle).Select(x => x).Remove();                Doc.Save(@"C:\books1.xml");                Result = true;                Console.WriteLine(Doc.ToString());            }            catch            {            }            finally            {                         }            return Result;        }        public static void InsertBook(Book Book)        {            XDocument Doc = XDocument.Load(@"C:\books1.xml");            XElement node=new XElement("book",new XAttribute("genre",Book.genre),                new XAttribute("publicationdate",Book.publicationdate),                new XAttribute("ISBN",Book.ISBN),                new XElement("title",Book.title),                new XElement("author",                    new XElement("first-name",Book.first_name),                    new XElement("last-name"),Book.last_name),                new XElement("price",Book.price));            Doc.Element(ElementNode).Add(node);            Doc.Save(XmlPath);        }        static void Main(string[] args)        {            ModifyPrice("Autobiography", 0.9);            Console.WriteLine(XDocument.Load(@"C:\books1.xml").ToString());            DeleteBook("The Confidence Man");            Console.ReadKey();
[解决办法]
C# code
    public class Book    {        public string genre { get; set; }        public string publicationdate { get; set; }        public string ISBN { get; set; }        public string title { get; set; }        public string first_name { get; set; }        public string last_name { get; set; }        public string price { get; set; }    } 


[解决办法]
好长,我有做过一个很简单的xml的例子,不知道和你的一样不一样,需要的话,可以短我

热点排行