XML & ASP.NET
XML DOM节点
<Document>
<DocumentType>
<XmlElement>
<XmlAttibte>
<XmlComment>
<XmlText>
XmlNode
XmlDocument模型
属性
HasChildNodes 有子节点返回true
ChildNodes 子节点集合
ParentNode 获取父节点 没有返回null
InnerXml 节点子节点串联值
InnerText 节点中标记值
OuterXml 该节点及子节点所有
NodeType 枚举值 Text Attribute Element
Value 根据类型返回值 Text->文本内容 Attribute->属性值 Elenment->null Entity->null
添加元素
XmlDocument doc = new
XmlDocument();
doc.Load("stus.xml");
XmlNode root =
doc.DocumentElement;
XmlElement element =
doc.CreateElement("Age");
element.InnerText =
"23";
root.AppendChild(element);
添加属性
XmlDocument doc = new
XmlDocument();
doc.Load("stus.xml");
XmlNode root =
doc.DocumentElement;
XmlAttribute attribute =
doc.CreateAttribute("Age");
attribute.Value =
"23";
root.Attribute.Append(attibute);
删除
XmlDocument doc = new
XmlDocument();
doc.Load("stus.xml");
XmlNode root =
doc.DocumentElement;
root.RemoveChild(root.ChildNodes[0]);
注:RemoveAll删除多有子节点
保存
XmlDocument doc = new
XmlDocument();
doc.load("stus.xml");
XmlElement element =
doc.CreateElement("name");
element.InnerText =
"zhang";
doc.DocumentElement.InsertAfter(element,
doc.DocumentElement.LastChild);
...
doc.Save(Server.MapPath("\stus.xml"))
XML->数据集
FileStream fs = new
FileStream(Server.MapPath("App_Data\\XMLFile.xml"), FileMode.Open,
FileAccess.Read);
StreamReader sr = new
StreamReader(fs);
DataSet ds = new
DataSet();
ds.ReadXml(sr);
DataView mySource = new
DataView(ds.Table[0]);
this.GridView1.DataSource
= mySource;
this.GridView1.DataBind();
数据集->XML
Dataset ds = new
DataSet();
SqlConnection conn = new
SqlConnection("server=.;database=my;uid=sa;pwd=123456");
SqlDataAdapter sda = new
SqlDataAdapter("SELECT * FROM students", conn);
sda.Fill(ds,
"stus");
ds.WriteXml("c:\MyStudents.xml");