[개발 노트] - LInq to xml 학습 노트

29358 단어 LINQ
최근 xml 문 서 를 조작 하 는 방법 이 필요 합 니 다. linq to xml 를 배 웠 습 니 다.
테스트 코드:
class Program
    {

        //  :  LINQ to XML      - luckdv -      http://www.cnblogs.com/luckdv/articles/1728088.html 


        static void Main(string[] args)
        {
            string path = @"E:\def\5.xml";
            //        
            //XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),new XElement("nianhui",new XElement("mingdan",new XText("      ")),new XElement("jiangxiang",new XText("     "))));
            //xdoc.Save(path);



            //        
            //XElement root = new XElement("nianhui",
            //    new XElement("first", "  "),
            //    new XElement("second", "   ")
            //    );

            //root.Save(path);


            //    Element
            //XElement rootload = XElement.Load(path);
            //XElement md = rootload.Element("mingdan");
            //if (md != null)
            //{
            //          
            //    md.ReplaceWith(new XElement("mingdan", "    "));

            //}
            //else
            //{
            //          
            //    rootload.Add(new XElement("mingdan", "     "));
            //}
            //rootload.Save(path);



            //     
            //XElement root=new XElement("nianhui");
            //root.Save(path);


            //  Value 
            //string x= GetElementValue(path,"mingda");
            //Console.WriteLine(x);


            //    
            //DelElement(path, "mingdan");

            //XElement root = XElement.Load(Xpath);
            //XElement element = root.Element(Xname);
            //if (element != null)
            //{
            //    element.Remove();
            //}
            ////                     
            //root.Save(Xpath);

            //          
            //XElement root = XElement.Load(path);
            //XElement element = root.Element("xing");
            //element.Add(new XElement("heihei","what"));
            //root.Save(path); 



            //------------    xml----------------------------------------------

            //      xml  
            //XElement xdoc = new XElement("root", new XElement("first",new XAttribute("ID","001")),new XElement("second","   value"));
            //xdoc.Save(path);

            //        xml  
            //XElement xdoc = new XElement("root",new XElement("first",new XAttribute("name","002"),new XAttribute("path","abc.txt")));
            //xdoc.Save(path);


            //       
            //XElement xdoc = XElement.Load(path);

            //       
            //XElement x = xdoc.Element("first");
            //string x2 = x.Attribute("path").Value;
            //Console.WriteLine(x2);

            //       
            //XElement x3 = xdoc.Element("first");
            //if (x3 != null)
            //{
            //    XAttribute a1 = x3.Attribute("path");
            //    if (a1 != null)
            //    {
            //        a1.SetValue("wohuanle");
            //    }
            //}
            //xdoc.Save(path);


            //       

            //XElement xdoc = XElement.Load(path);
            //XElement x4 = xdoc.Element("first");
            //if (x4 != null)
            //{
            //    XAttribute a2 = x4.Attribute("path");
            //    if (a2 != null)
            //    {
            //        Console.WriteLine(a2.Value);
            //    }
            //    else
            //    {
            //        Console.WriteLine("");
            //    }
            //}


            //        
            //XElement xdoc = XElement.Load(path);
            //XElement x4 = xdoc.Element("first");
            //if (x4 != null)
            //{
            //    x4.SetAttributeValue("hah", "what");
            //}
            //xdoc.Save(path);


            //                     
            //XElement xdoc = XElement.Load(path);
            //XElement x4 = xdoc.Element("mingdan");
            //if (x4 != null)
            //{
            //    XAttribute attr = x4.Attribute("number");
            //    if (attr != null)
            //    {
            //        //     
            //        attr.SetValue("woshixinzengjiade");
            //    }
            //    else
            //    {
            //        //      
            //        x4.SetAttributeValue("number", "meizhegeshuxing");
            //    }
            //}
            //xdoc.Save(path);


            //    xml                
            //XElement xdoc = XElement.Load(path);
            ////XElement x4 = xdoc.Element("mingdan");

            //IEnumerable<XElement> element = from e in xdoc.Elements()
            //             where e.Elements().Any(c=>c.Name=="xing")
            //              select e;
            //foreach (XElement it in element)
            //{
            //  Console.WriteLine(  it.Value);
            //}


            //          
            //Dictionary<string,string> ss=new Dictionary<string,string>();
            //ss.Add("na","01");ss.Add("nb","02");
            //CreateAttr(path, "heihei",ss);


            //           
            XElement xdoc = XElement.Load(path);
            XElement x4 = xdoc.Element("mingdan");
            if (x4 != null)
            {
                if (x4.Attribute("number") != null)
                {
                    x4.Attribute("number").Remove();
                }
            }
            xdoc.Save(path);


            Console.ReadKey();

        }





        #region           
        /// <summary>
        ///           
        /// </summary>
        /// <param name="Xpath">    </param>
        /// <param name="Xname">      </param>
        /// <param name="Attr">   </param>
        static void CreateAttr(string Xpath, string Xname, Dictionary<string, string> Attr)
        {
            XElement xdoc = XElement.Load(Xpath);
            XElement x4 = xdoc.Element(Xname);
            if (x4 != null)
            {
                foreach (var item in Attr)
                {
                    x4.Add(new XAttribute(item.Key, item.Value));
                }
            }
            xdoc.Save(Xpath);
        }
        #endregion



        #region        Xroot xml  
        /// <summary>
        ///        Xroot xml  
        /// </summary>
        /// <param name="Xpath"></param>
        /// <param name="Xroot"></param>
        void InitElement(string Xpath, string Xroot)
        {
            XElement root = new XElement(Xroot);
            root.Save(Xpath);
        }
        #endregion



        #region        
        /// <summary>
        ///        
        /// </summary>
        /// <param name="Xpath"></param>
        /// <param name="Xname"></param>
        /// <param name="XValue"></param>
        void CreateElement(string Xpath, string Xname, string XValue)
        {
            //  xml    
            XElement root = XElement.Load(Xpath);
            //       name   
            XElement first = root.Element(Xname);
            if (first != null)
            {
                //
                first.ReplaceWith(new XElement(Xname, XValue));
            }
            else
            {
                //
                root.Add(new XElement(Xname, XValue));
            }
            //        
            root.Save(Xpath);
        }
        #endregion


        #region       Value 
        /// <summary>
        ///       Value 
        /// </summary>
        /// <param name="Xpath"></param>
        /// <param name="Xname"></param>
        /// <returns></returns>
        static string GetElementValue(string Xpath, string Xname)
        {
            XElement root = XElement.Load(Xpath);
            XElement element = root.Element(Xname);
            if (element != null)
            {
                return element.Value;
            }
            else
            {
                return "";
            }

        }
        #endregion


        #region   Name Xname    
        /// <summary>
        ///   Name Xname    
        /// </summary>
        /// <param name="Xpath"></param>
        /// <param name="Xname"></param>
        static void DelElement(string Xpath, string Xname)
        {
            XElement root = XElement.Load(Xpath);
            XElement element = root.Element(Xname);
            if (element != null)
            {
                element.Remove();
            }
            //    
            root.Save(Xpath);
        }
        #endregion

    }

 
2014 - 1 - 10 10: 08: 37 하나 추가:
        /// <summary>
        ///          (1    )
        /// </summary>
        /// <param name="Xpath"></param>
        /// <param name="Xelement"></param>
      static void AddElement(string Xpath, Dictionary<string, string> Xelement)
      {
          //  xml    
          XElement root = XElement.Load(Xpath);
          foreach (var e in Xelement)
          {
              //            ,   ,      ,    ,   
              XElement first = root.Element(e.Key);
              if (first != null)
              {
                  first.SetValue(e.Value);
              }
              else
              {
                  root.SetElementValue(e.Key, e.Value);
              }
          }
          root.Save(Xpath);
      }

 
 
  참고: LINQ to XML 프로 그래 밍 기초 - luckdv - 블 로그 원 http://www.cnblogs.com/luckdv/articles/1728088.html 
           
전재 출처 를 밝 혀 주 십시오.

좋은 웹페이지 즐겨찾기