.net与XML——LINQ To Xml
入门的,先举一例
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace mylqConApp
{
class lqxml
{
static void Main()
{
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
//元素节点
new XElement("books",
new XElement("Book",
new XAttribute("ID", "1"),
new XElement("NO", "1254-004"),
new XElement("Name", "Book 004"),
new XElement("Price", "30.00"),
new XElement("Remark", "this is a book about conputer")
),
new XElement("Book",
new XAttribute("ID", "2"),
new XElement("NO", "1254-005"),
new XElement("Name", "Book 005"),
new XElement("Price", "35.00"),
new XElement("Remark", "this is a book about conputer")
),
new XElement("Book",
new XAttribute("ID", "3"),
new XElement("NO", "1254-015"),
new XElement("Name", "Book 015"),
new XElement("Price", "135.00"),
new XElement("Remark", "this is a book about conputer")
)
)
);
//Serialize this System.Xml.Linq.XDocument to a file.
doc.Save("c:\\tt.xml");
//Creates a new System.Xml.Linq.XDocument from a file.
XDocument xdoc = XDocument.Load("c:\\tt.xml");
Console.WriteLine(xdoc.ToString());
//查询根元素名称
IEnumerable<XElement> root = from e in xdoc.Elements("books")
select e;
foreach (var s in xdoc.Elements())
{
Console.WriteLine(s.Name);
}
foreach (XElement xe in root)
{
Console.WriteLine("查询根元素名称 " + xe.Name);
}
//Loads an System.Xml.Linq.XElement from a file
XElement xElem = XElement.Load("c:\\tt.xml");
//查询指定名称的元素
IEnumerable<XElement> ele = from e in xElem.Elements("Book")
where (string)e.Element("Name") == "Book 005"
select e;
//或者
//IEnumerable<XElement> ele = from e in xdoc.Elements("books").Elements("Book")
// where (string)e.Element("Name") == "Book 005"
// select e;
foreach (var u in ele)
{
Console.WriteLine("ID " + u.Attribute("ID").Value + " name " + u.Name);
}
//Add
XElement book = new XElement("Book",
new XAttribute("ID", "4"),
new XElement("NO", "1254-045"),
new XElement("Name", "Book 045"),
new XElement("Price", "135.00"),
new XElement("Remark", "this is a book about conputer"));
xElem.Add(book);
//生成添加新元素之后的xml文件
xElem.Save("c:\\t.xml");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace mylqConApp
{
class lqxml
{
static void Main()
{
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
//元素节点
new XElement("books",
new XElement("Book",
new XAttribute("ID", "1"),
new XElement("NO", "1254-004"),
new XElement("Name", "Book 004"),
new XElement("Price", "30.00"),
new XElement("Remark", "this is a book about conputer")
),
new XElement("Book",
new XAttribute("ID", "2"),
new XElement("NO", "1254-005"),
new XElement("Name", "Book 005"),
new XElement("Price", "35.00"),
new XElement("Remark", "this is a book about conputer")
),
new XElement("Book",
new XAttribute("ID", "3"),
new XElement("NO", "1254-015"),
new XElement("Name", "Book 015"),
new XElement("Price", "135.00"),
new XElement("Remark", "this is a book about conputer")
)
)
);
//Serialize this System.Xml.Linq.XDocument to a file.
doc.Save("c:\\tt.xml");
//Creates a new System.Xml.Linq.XDocument from a file.
XDocument xdoc = XDocument.Load("c:\\tt.xml");
Console.WriteLine(xdoc.ToString());
//查询根元素名称
IEnumerable<XElement> root = from e in xdoc.Elements("books")
select e;
foreach (var s in xdoc.Elements())
{
Console.WriteLine(s.Name);
}
foreach (XElement xe in root)
{
Console.WriteLine("查询根元素名称 " + xe.Name);
}
//Loads an System.Xml.Linq.XElement from a file
XElement xElem = XElement.Load("c:\\tt.xml");
//查询指定名称的元素
IEnumerable<XElement> ele = from e in xElem.Elements("Book")
where (string)e.Element("Name") == "Book 005"
select e;
//或者
//IEnumerable<XElement> ele = from e in xdoc.Elements("books").Elements("Book")
// where (string)e.Element("Name") == "Book 005"
// select e;
foreach (var u in ele)
{
Console.WriteLine("ID " + u.Attribute("ID").Value + " name " + u.Name);
}
//Add
XElement book = new XElement("Book",
new XAttribute("ID", "4"),
new XElement("NO", "1254-045"),
new XElement("Name", "Book 045"),
new XElement("Price", "135.00"),
new XElement("Remark", "this is a book about conputer"));
xElem.Add(book);
//生成添加新元素之后的xml文件
xElem.Save("c:\\t.xml");
}
}
}
真的是入门级的,自己留着看看……