eXtensiableMarkupLanguage基础
目的:描述数据,程序,机器
语法: <element s1="a" s2="b"/>
命名空间:xmlns
架构:schema 是一种规范
例如
生成构架:先写好一个单内容的XML,点击XML---生成构架
1 <?xml version="1.0" encoding="utf-8"?>
2 <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
3 <xs:element name="net">
4 <xs:complexType>
5 <xs:sequence>
6 <xs:element maxOccurs="unbounded" name="student">
7 <xs:complexType>
8 <xs:simpleContent>
9 <xs:extension base="xs:string">
10 <xs:attribute name="name" type="xs:string" use="required" />
11 <xs:attribute name="age" type="xs:unsignedByte" use="required" />
12 </xs:extension>
13 </xs:simpleContent>
14 </xs:complexType>
15 </xs:element>
16 </xs:sequence>
17 </xs:complexType>
18 </xs:element>
19 </xs:schema>
2 <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
3 <xs:element name="net">
4 <xs:complexType>
5 <xs:sequence>
6 <xs:element maxOccurs="unbounded" name="student">
7 <xs:complexType>
8 <xs:simpleContent>
9 <xs:extension base="xs:string">
10 <xs:attribute name="name" type="xs:string" use="required" />
11 <xs:attribute name="age" type="xs:unsignedByte" use="required" />
12 </xs:extension>
13 </xs:simpleContent>
14 </xs:complexType>
15 </xs:element>
16 </xs:sequence>
17 </xs:complexType>
18 </xs:element>
19 </xs:schema>
使用此架构后:
新建XML,选择构架属性
<?xml version="1.0" encoding="utf-8" ?>
<net>
<student name="张三" age="19">
</student>
</net>
<net>
<student name="张三" age="19">
</student>
</net>
XmlReader操作
1 var filename = Server.MapPath("~/Xml/demo.xml");
2 filename = "http://www.verycd.com/sto/feed";
3 var reader = XmlReader.Create(filename);
4 var count = 0;
5 while (reader.Read())
6 {
7 if (reader.NodeType == XmlNodeType.Element && reader.Name == "item")
8 {
9 count++;
10 }
11 }
12 this.Literal1.Text = string.Format("共有{0}条", count);
2 filename = "http://www.verycd.com/sto/feed";
3 var reader = XmlReader.Create(filename);
4 var count = 0;
5 while (reader.Read())
6 {
7 if (reader.NodeType == XmlNodeType.Element && reader.Name == "item")
8 {
9 count++;
10 }
11 }
12 this.Literal1.Text = string.Format("共有{0}条", count);
页面:
1 <asp:Xml ID="Xml1" runat="server" DocumentSource="~/Xml/demo.xml"></asp:Xml>
2 <asp:Literal ID="Literal1" runat="server"></asp:Literal>
2 <asp:Literal ID="Literal1" runat="server"></asp:Literal>