您的位置:首页 > 编程语言 > ASP

ASP.NET XML与JSON

2013-12-20 21:22 363 查看
1, JS对HTML的操作是通过DOM来执行的,即JS通过DOM可以对HTML文档中的所有元素进行增删改查移,DOM给出了HTML文档中所有元素的访问入口,并提供了对这些元素进行增删改查移的方法和属性。

2,

//实例化一个XmlDocument
XmlDocument doc = new XmlDocument();
string fileName = Server.MapPath(@"~\XML\XMLFile1.xml");
//加载XmlDocument
doc.Load(fileName);
//得到文档的根节点
XmlNode root = doc.DocumentElement;
string info = "";
foreach (XmlNode item in root.ChildNodes)
{
info += item.Attributes.Item(0).Value;
info += item.Attributes.Item(1).Value;
foreach (XmlNode items in item.ChildNodes)
{
info += items.Value;
}
info += "<br/>";
}
Response.Write(info);


3,

//创建XML文档对象
XmlDocument doc = new XmlDocument();
//创建XML文档描述
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0","utf-8",null);

//创建根节点
XmlNode root = doc.CreateNode(XmlNodeType.Element,"classes",null);
//创建子节点
XmlNode class1 = doc.CreateNode(XmlNodeType.Element,"class1",null);
XmlNode name = doc.CreateNode(XmlNodeType.Text,null,null);
name.Value = "张三";

XmlAttribute att = doc.CreateAttribute("stuno");
att.Value = "007";
XmlAttribute atts = doc.CreateAttribute("age");
atts.Value = "88";

//在根节点添加班级节点
class1.AppendChild(name);
class1.Attributes.Append(att);
class1.Attributes.Append(atts);

root.AppendChild(class1);
//在文档中添加文档描述
doc.AppendChild(declaration);
//在文档中添加根节点
doc.AppendChild(root);
//保存文档
string filename = Server.MapPath(@"~\XML\classsss.xml");
doc.Save(filename);


4,xml:本身是一个文本文件,用于存储数据的,有人把XML当成小型数据库来使用.

5.DOM树中节点:元素节点

        属性节点

        文本节点

        注释节点

        文档节点

6.

//创建xml文档对象
XmlDocument doc = new XmlDocument();
//创建xml文档描述
XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
//创建根节点
XmlNode root = doc.CreateNode(XmlNodeType.Element, "employee", null);

List<students> list = new List<students> { new students() {name = "张三", age = "20" }, new students() {name = "李四", age = "88" } }; ;
foreach (var item in list)
{
//创建子节点
XmlNode xx = doc.CreateNode(XmlNodeType.Element, "student", null);
XmlAttribute dd = doc.CreateAttribute("name");
dd.Value = item.name;
XmlAttribute ds = doc.CreateAttribute("age");
ds.Value = item.age;
xx.Attributes.Append(dd);
xx.Attributes.Append(ds);
root.AppendChild(xx);
}
doc.AppendChild(declaration);
doc.AppendChild(root);

string path = Server.MapPath(@"~/List/XMLFile1.xml");
doc.Save(path);


7,

//将dataset转换为list
DataSet dd = Method();
List<students> list = new List<students>();
foreach (DataRow item in dd.Tables[0].Rows)
{
students stu = new students();
stu.name = item["name"].ToString();
list.Add(stu);
}
//将list转化为dataset
foreach (var item in list)
{
DataSet da = new DataSet();
DataTable dt = new DataTable();
da.Tables.Add(dt);
dt.Columns.Add("name");
dt.Rows.Add(item.name);
}
}
private DataSet Method()
{
string sql = "select * from students";
SqlConnection con = new SqlConnection("server=.;database=stuDB;uid=sa;pwd=123456");
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: