| 代码如下 | 复制代码 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace LinqToXml2
{
class Program
{
static void Main(string[] args)
{
XDocument doc = new XDocument(
new XDeclaration("1.0", "UTF-8", "true"),
new XElement("Root00",
new XElement("Root10",
new XAttribute("color", "Green"), "椭圆"),
new XElement("Root11",
new XAttribute("color", "White"),
new XAttribute("value", "四边形"),
new XElement("Root20",
new XAttribute("color", "Purple"), "菱形"
),
new XElement("Root21",
new XAttribute("color", "Red"), "梯形")),
new XElement("Root12",
new XAttribute("color", "Orange"), "三角形")
)
);
doc.Save("http://www.c**nb*logs.com/Shape.xml");//用此方法来保存xml文档到文件当中
}
}
}
|
|
| 代码如下 | 复制代码 |
|
|
第一句( ),这是声明为XML的语句,它的作用就是告诉浏览器将要处理的文档是XML文件。但是XML声明在XML文档里面是可选的,也就是说不出现XML的声明语句,有时候也是可以的。但是在我们这里就不可以了,因为我们的XML文档里面包含中文(中文的编码是GBK、GB2312或者是utf-8),如果不声明的话,在浏览器里面打开会报错。
第二句(
第三句(
好了,双击Shape.xml,你可以看到下图的显示结果: ),这里面Root10是根元素Root00的子元素,color是
| 代码如下 | 复制代码 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace LinqToXml2
{
class Program
{
static void Main(string[] args)
{
XDocument shapeDoc = XDocument.Load("http://www.c**nb*logs.com/Shape.xml");
Console.WriteLine(shapeDoc);
//XElement shapeElement = XDocument.Load("http://www.c**nb*logs.com/Shape.xml");
//Console.WriteLine(shapeElement);
}
}
}
|
|
| 代码如下 | 复制代码 |
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace LinqToXml2 { class Program { static void Main(string[] args) { XElement doc = XElement.Load("http://www.c**nb*logs.com/Shape.xml"); IEnumerable
|
|
查询,这是遍历XML文档里面的元素,这里面输出的结果如下所示。
| 代码如下 | 复制代码 |
|
|
| 代码如下 | 复制代码 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace LinqToXml2
{
class Program
{
static void Main(string[] args)
{
XElement doc = XElement.Load("http://www.c**nb*logs.com/Shape.xml");
XElement add = new XElement("Root13",
new XAttribute("Color", "Yellow"), "星形");
doc.
Add(add);
//doc.Element("Root10").AddBeforeSelf(add);
//doc.Element("Root10").AddAfterSelf(add);
var elements = from e in doc.Elements()
select(e);
foreach (var elment in elements)
{
Console.WriteLine(elment);
}
Console.Read();
}
}
}
|
|
创造与魔法九游版本
创造与魔法九游版采用了极致创新的魔幻游戏玩法感受,游戏具备最
疯狂医院达什医生中文版(Crazy Hospital)
疯狂医院达什医生最新版是一款医院模拟经营类游戏,逼真的场景画
宝宝庄园官方版
宝宝庄园官方版是一款超级经典好玩的模拟经营类型的手游,这个游
桃源记官方正版
桃源记是一款休闲娱乐类的水墨手绘风格打造的模拟经营手游。玩家
长途巴士模拟器手机版
长途巴士模拟器汉化版是一款十分比真好玩的大巴车模拟驾驶运营类