linq to xml

          //创建xml
          XDocument employeeDoc =
             new XDocument(
                new XElement("html",
                   new XElement("body",
                      new XElement("html", "html"),
                      new XElement("head", "")),
                   new XElement("body",
                      new XElement("html", "html5"),
                      new XElement("head", "")
                      )
                   )
                );

          employeeDoc.Save("sp.xml");


          XElement root = employeeDoc.Element("html");
          //XElement root = employeeDoc.Element("html");//为什么这里写body就是null呢?难道一定要是根节点吗 求解释

          var sp = "我是所有标签body的值 ";
          var sp1 = "我是所有标签html的值 ";
          var sp2 = "我是所有标签head的值 ";
          IEnumerable<XElement> employees = root.Elements("body");
          foreach (XElement emp in employees)
          {
              var xyz = from e in emp.Elements()
                        select e.Value;
              foreach (var x in xyz)
                  sp += x + ",";


              XElement empNameNode = emp.Element("html");
              sp1 += empNameNode.Value + ",";

              IEnumerable<XElement> head = emp.Elements("head");
              foreach (XElement phone in head)
                  sp2 += phone.Value + ",";
          }

          Console.WriteLine(sp);
          Console.WriteLine(sp1);
          Console.WriteLine(sp2);


          //查询标签所有的值
         XDocument xd = XDocument.Load("SimpleSample.xml" ); 
         XElement rt = xd.Element("MyElements" );            
         var xyz1 = from e in rt.Elements()
                   select e.Value;
         foreach ( var x in xyz1 )
            Console.WriteLine( x );

c# 

//创建XmlDocument对象

XmlDocument xmlDoc = new XmlDocument();

//载入xml文件名

xmlDoc.Load(filename);

//如果是xml字符串,则用以下形式

xmlDoc.LoadXml(xmldata);

//读取根节点的所有子节点,放到xn0中

XmlNodeList xn0 = xmlDoc.SelectSingleNode("Document").ChildNodes;

//查找二级节点的内容或属性

foreach (XmlNode node in xn0)

{

    if (node.Name == 匹配的二级节点名)

    {

        string innertext = node.InnerText.Trim(); //匹配二级节点的内容

        string attr = node.Attributes[0].ToString(); //属性

    }

}

基础的操作就是这些,要是取三级或三级以上的节点,类似推导即可

 

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml("");
            XmlNodeList xn0 = xmlDoc.SelectSingleNode("GeocodeResponse").ChildNodes;
            foreach (XmlNode node in xn0)
            {

                if (node.Name == "status")
                {
                    if (node.InnerText.Trim().ToLower() != "ok")
                    {
                        break;
                    }
                }
                if (node.Name == "result")
                {

                    XmlNode xnAddress = node.SelectSingleNode("formatted_address");
                    string address = xnAddress.InnerText.Trim(); //匹配二级节点的内容
                    string attr = node.Attributes[0].ToString(); //属性
                    break;
                }
            }

 

 

            if (LongOnLineTabOneDt.Table == null || LongOnLineTabOneDt.Table.Rows.Count == 0) return;
            XElement root = XElement.Load(xmlPath);
            var tempDom = root.Element("OnLineRateTabOne");
            tempDom.Element("Range").Attribute("ColumnCount").SetValue(LongOnLineTabOneDt.Table.Rows.Count);
            var table = tempDom.Element("Table");
            List<XElement> tempRemove = new List<XElement>();
            foreach (XElement item in table.Nodes())
            {
                var temps = ((System.Xml.Linq.XElement)(item)).FirstAttribute.Value;
                if (temps != "所属公司" && temps != "车牌号码" && temps != "车牌颜色")
                {
                    tempRemove.Add(item);
                }
            }
            tempRemove.ForEach((x) =>
            {
                x.Remove();
            });

            var tempColumnsIndex = 0;
            foreach (DataColumn dc in LongOnLineTabOneDt.Table.Columns)
            {
                tempColumnsIndex++;
                if (tempColumnsIndex > 3)
                {
                    table.LastNode.AddAfterSelf(new XElement("Column",
                        new XAttribute("Name", "" + dc.ColumnName + ""),
                        new XAttribute("BgColor", "LightSteelBlue"),
                        new XAttribute("Width", "20"),
                        new XAttribute("Text", "" + dc.ColumnName + ""),
                        new XAttribute("ContentHorizonAlign", "left")));
                }
            }
            root.Save(xmlPath);

 

posted @ 2013-06-06 16:57  s_p  阅读(181)  评论(0编辑  收藏  举报