zoukankan      html  css  js  c++  java
  • LinqToXml

    简单的创建一个Xml

                ///创建一个Xml文档
                XElement x = new XElement("qiao");//创建一个根节点
                var xx = new XElement("job");
                xx.Add(new XElement("xx1", "xx1"));
                xx.Add(new XElement("xx2", "xx2"));
                xx.Add(new XElement("xx3", "xx3"));
                x.Add(xx);
                var aa = new XElement("job1");
                aa.Add(new XElement("xx1", "xx1"));
                aa.Add(new XElement("xx2", "xx2"));
                aa.Add(new XElement("xx3", "xx3"));
                x.Add(aa);
    
                x.Save("F:\12.xml");

    把一个Xml文件转换成一个数据实体

            XElement x = XDocument.Load("F:\13.xml").Root.Element("{www.qiao.con}jj");
                
                Student stu = new Student()
                {
                    ID = Convert.ToInt32(x.Element("{www.qiao.con}ID").Value),
                    Name = x.Element("{www.qiao.con}Name").Value,
                    Age = Convert.ToInt32(x.Element("{www.qiao.con}Age").Value),
                    Gender = Convert.ToInt32(x.Element("{www.qiao.con}Gender").Value),
                };

    在这个Xml后面追加一个相同的节点

                Student s = new Student() { Age = 22, ID = 1, Gender = 1, Name = "张三" };
    
                XElement xe = XDocument.Load("F:\13.xml").Root;
                var c = new XElement("{www.qiao.con}jj");
                c.Add(new XElement("{www.qiao.con}ID", s.ID));
                c.Add(new XElement("{www.qiao.con}Name", s.Name));
                c.Add(new XElement("{www.qiao.con}Age", s.Age));
                c.Add(new XElement("{www.qiao.con}Gender", s.Gender));
                xe.Add(c);
                xe.Save("F:\13.xml");

    获取到ID == 1的那条数据

    IEnumerable<XElement> listXml = XDocument.Load("F:\13.xml").Root.Elements("{www.qiao.con}jj");
                XElement xe = listXml.Where(x => x.Element("{www.qiao.con}ID").Value == "1").FirstOrDefault();
  • 相关阅读:
    Codeforces 1072
    XDOJ 1046
    BZOJ 1002
    BZOJ 1001
    BZOJ 1500/Luogu 2042
    nowcoder 211B
    BZOJ 3224
    BZOJ 1150
    POJ 2442
    二叉堆和二叉搜索树进阶
  • 原文地址:https://www.cnblogs.com/ansheng/p/5482532.html
Copyright © 2011-2022 走看看