linq to xml 實戰

以前用xml 寫程序一直用傳統的方法,這幾天做一個程序決定用linq to xml ,通過實戰,把linq to xml各種用法已經搞得比較熟了,下面介紹一下:
      比如要生成象下面那樣的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<tags>
  <tag id="1">
    <tagid>1</tagid>
    <tagname>ibm專區雲標籤</tagname>
    <tagheight>200</tagheight>
    <tagwidth>300</tagwidth>
    <cloudxmlfile>tagcloud1.xml</cloudxmlfile>
  </tag>
  <tag id="2">
    <tagid>2</tagid>
    <tagname>CAD專題</tagname>
    <tagheight>300</tagheight>
    <tagwidth>500</tagwidth>
    <cloudxmlfile>tagcloud2.xml</cloudxmlfile>
  </tag>
</tags>
可以象下面這樣寫:
   public void AddTagNameXml(string tagname,string tagheight,string tagwidth)
      {
         string xmlFilePath =  "/xml/tagcloudnamexml.xml";
        //這個地方還應該加上驗證文件存不存在的代碼,這裏省略
          XElement xe = XElement.Load(HttpContext.Current.Server.MapPath(xmlFilePath));
          int tagid = 0;     
          int topxecount= xe.Elements("tag").Count();
        //看是否存在tag節點
          if (topxecount > 0)
          {
              XElement tagidelement = xe.Elements("tag").Last().Element("tagid");
             //給tag標籤tag節點的屬性id+1
              tagid = int.Parse(tagidelement.Value) + 1;
          }
          else
              tagid = 1;
          //給雲標籤xml文件取名
          string tagcloudXmlName = "tagcloud" + tagid + ".xml";   
              XElement tag = new XElement("tag",
                new XAttribute("id", tagid),
               new XElement("tagid", tagid),
               new XElement("tagname", tagname),
                 new XElement("tagheight", tagheight),
               new XElement("tagwidth", tagwidth),
                new XElement("cloudxmlfile", tagcloudXmlName)
               );
              xe.Add(tag);                
          xe.Save(HttpContext.Current.Server.MapPath(xmlFilePath));
              }
//修改
  private void ModifyXmlElement(string tagid, string tagname, string tagheight, string tagwidth)
      {
        string xmlFilePath =  "/xml/tagcloudnamexml.xml";
          XElement xe = XElement.Load(HttpContext.Current.Server.MapPath(xmlFilePath));
          IEnumerable<XElement> element = from e in xe.Elements("tag")
                                          where e.Attribute("id").Value == tagid
                                          select e;
          if (element.Count() > 0)
          {
              XElement firstelement = element.First();
              firstelement.Element("tagname").ReplaceWith(new XElement("tagname", tagname));
              firstelement.Element("tagheight").ReplaceWith(new XElement("tagheight", tagheight));
              firstelement.Element("tagwidth").ReplaceWith(new XElement("tagwidth", tagwidth));                     
          }
          xe.Save(HttpContext.Current.Server.MapPath(xmlFilePath));
      }
/// <summary>
      /// 刪除
      /// </summary>
      /// <param name="attrvalue">節點屬性值,用來查找要刪除的節點</param>
      /// <param name="xmlPath">xml文件路徑</param>
      /// <param name="elementname">節點名稱</param>
      /// <param name="attrname">屬性名稱</param>
      private void DeleteXmlElement(string attrvalue,string xmlPath,string elementname,string attrname)
      {
          XElement xe = XElement.Load(HttpContext.Current.Server.MapPath(xmlPath));
          IEnumerable<XElement> element = from e in xe.Elements(elementname)
                                          where e.Attribute(attrname).Value == attrvalue
                                          select e;
          if (element.Count() > 0)
          {
              element.Remove();
          }
          xe.Save(HttpContext.Current.Server.MapPath(xmlPath));
      }
再比如要生成下面的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<tags>
  <tag tagid="2" tagcontent="智能企業" tagurl="http://ibm.e-works.net.cn/document/200908/article8963.htm" />
  <tag tagid="3" tagcontent="中小企業" tagurl="http://ibm.e-works.net.cn/document/200908/article8969.htm" />
  <tag tagid="4" tagcontent="中望cad2" tagurl="http://www.e-works.net.cn" />
  <tag tagid="5" tagcontent="" tagurl="" />
  <tag tagid="6" tagcontent="yy" tagurl="dgd" />
</tags>

   private string AddCloudTagXmlForList(string tagcontent,tstring agurl)
      {
          string xmlFilePath =  "/xml/tagcloudxml.xml";     
          XElement tag = XElement.Load(PathHelper.Map(xmlpath));
          int topxecount = tag.Elements("tag").Count();
          int tagid = 0;
          if (topxecount > 0)
          {
              string maxtagid = tag.Elements("tag").Last().Attribute("tagid").Value;
              tagid = int.Parse(maxtagid) + 1;
          }
          else
             tagid = 1;
          tag.Add(new XElement("tag", new XAttribute("tagid", tagid), new XAttribute("tagcontent", tagcontent), new XAttribute("tagurl", tagurl)));             
           tag.Save(PathHelper.Map(xmlpath));
           return xmlpath;
        
      }
   //修改    
private void ModifyCloudTagXmlForListElement(string tagcontent,string tagurl,string xmlpath)
      {
          XElement tag = XElement.Load(PathHelper.Map(xmlpath));
          IEnumerable<XElement> element = from e in tag.Elements("tag")
                                          where e.Attribute("tagid").Value == ctag.tagid.ToString()
                                          select e;
          if (element.Count() > 0)
          {
              XElement firstelement = element.First();
              firstelement.Attribute("tagcontent").SetValue(tagcontent);
              firstelement.Attribute("tagurl").SetValue(tagurl);
           
          }
          tag.Save(PathHelper.Map(xmlpath));
      }
再比如象下面形式的xml
<?xml version="1.0" encoding="utf-8"?>
<tags>
  <a tagid="2" href="http://ibm.e-works.net.cn/document/200908/article8963.htm" style="font-size:18pt;">智能企業</a>
  <a tagid="3" href="http://ibm.e-works.net.cn/document/200908/article8969.htm" style="font-size:18pt;">中小企業</a>
  <a tagid="4" href="http://www.e-works.net.cn" style="font-size:18pt;">中望cad2</a>
</tags>
public void AddCloudTagXml(string tagcontent,string tagurl)
      {
               XElement xe = XElement.Load(PathHelper.Map(xmlpath));
               int topxecount = xe.Elements("a").Count();
          int tagid = 0;
          if (topxecount > 0)
          {
              string maxtagid = xe.Elements("a").Last().Attribute("tagid").Value;
              tagid = int.Parse(maxtagid) + 1;
          }
          else
             tagid = 1;
          XElement tagadd =
           new XElement("a", new XAttribute("tagid", tagid), new XAttribute("href", tagurl), new XAttribute("style", "font-size:18pt;"), tagcontent);
          xe.Add(tagadd);
          xe.Save(PathHelper.Map(xmlpath));                  
      }
//修改
  private void ModifyCloudTagXmlElement(string tagcontent,string tagurl, string xmlpath)
      {
                XElement tag = XElement.Load(PathHelper.Map(xmlpath));
          IEnumerable<XElement> element = from e in tag.Elements("a")
                                          where e.Attribute("tagid").Value == ctag.tagid.ToString()
                                          select e;
          if (element.Count() > 0)
          {
              XElement firstelement = element.First();
              firstelement.SetValue(tagcontent);
              firstelement.Attribute("href").SetValue(tagurl);

          }
          tag.Save(PathHelper.Map(xmlpath));
      } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章