• 周日. 5月 26th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Xml基础

admin

11月 28, 2021

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;

namespace Comp.WEB.Xml
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

//创建
protected void btn_Click(object sender, EventArgs e)
{
#region 创建

//<?xml version=”1.0″ encoding=”utf-8″?>
// <Books>
// <Book>
// <Name>红楼梦</Name>
// <Price />
// </Book>
// </Books>

// //xml 文档
// XmlDocument doc = new XmlDocument();
// //创建文档声明节点

// XmlDeclaration xmlDec= doc.CreateXmlDeclaration(“1.0”, “utf-8”, null);
// doc.AppendChild(xmlDec);
// //创建根节点
// XmlElement parent = doc.CreateElement(“Books”);
// doc.AppendChild(parent);
// //Books/Book
// XmlElement book1 = doc.CreateElement(“Book”);
// parent.AppendChild(book1);

// //Book/Name
// XmlElement name = doc.CreateElement(“Name”);
// name.InnerText = “红楼梦”;
//book1.AppendChild(name);
// //Book/Price
// XmlElement prizr=doc.CreateElement(“Price”);
// prizr.InnerText = “10”;
// book1.AppendChild(prizr);
// doc.Save(“c:\1.xml”);

//补充
//XmlElement book1 = doc.CreateElement(“Book”);
//book1.InnerText = “<Name>红楼梦</Name><Prize>10</Prize>”; 不会转义
//book1.InnerXml = “<Name>红楼梦</Name><Prize>10</Prize>”;自动转义

#endregion
//创建
XmlDocument doc = new XmlDocument();
string path = “c:\1.xml”;
if (!File.Exists(path))
{
//创建声明
XmlDeclaration xmlDec = doc.CreateXmlDeclaration(“1.0”, “utf-8”, null);
//添加声明
doc.AppendChild(xmlDec);
//创建BOOKS 节点
XmlElement parent = doc.CreateElement(“Books”);
//添加Books节点
doc.AppendChild(parent);
//添加Books/Book下面的节点
XmlElement book1 = doc.CreateElement(“Book”);
parent.AppendChild(book1);

XmlElement name = doc.CreateElement(“Name”);
name.InnerText = “红楼梦”;
book1.AppendChild(name);
XmlElement Price = doc.CreateElement(“Price”);
Price.InnerText = “10”;
book1.AppendChild(Price);

}
else
{
//更新
//加载已经存在的xml文件
doc.Load(path);
// //获取books节点 根节点
XmlElement parent = doc.DocumentElement;

// //创建Book
XmlElement book1 = doc.CreateElement(“Book”);

//添加BOOk节点
parent.AppendChild(book1);
//添加Name 节点
XmlElement name = doc.CreateElement(“Name”);
name.InnerText = “西游记”;
book1.AppendChild(name);

XmlElement price = doc.CreateElement(“Price”);
price.InnerText = “20”;
book1.AppendChild(price);

}

doc.Save(“c:\1.xml”);

// <?xml version=”1.0″ encoding=”utf-8″?>
//<Books>
// <Book>
// <Name>红楼梦</Name>
// <Price>10</Price>
// </Book>
//</Books>

}

/// <summary>
/// 创建带属性的节点
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
protected void btn2_Click(object sender, EventArgs e)
{

//<Order>
// <CustomerName>nl</CustomerName>
// <OrderNumber>BJ200888</OrderNumber>
// <Items>
// <OrderItem Name=”电脑” Count=”30″/>
// <OrderItem Name=”电视” Count=”2″/>
// <OrderItem Name=”水杯” Count=”20″/>
// </Items>
//</Order>
XmlDocument doc = new XmlDocument();

XmlDeclaration xmldec = doc.CreateXmlDeclaration(“1.0”, “utf-8”, null);
doc.AppendChild(xmldec);

//1创建父节点Order
XmlElement ordre = doc.CreateElement(“Order”);
doc.AppendChild(ordre);
//2创建CustomerName
XmlElement CustomerName = doc.CreateElement(“CustomerName”);
CustomerName.InnerText = “n1”;
ordre.AppendChild(CustomerName);
//3 //创建OrderNumber
XmlElement OrderNumber = doc.CreateElement(“OrderNumber”);
OrderNumber.InnerText = “BJ200088”;
ordre.AppendChild(OrderNumber);
////4创建Items
XmlElement Items = doc.CreateElement(“Items”);
ordre.AppendChild(Items);
//5//创建OrderItem
XmlElement orderItem1 = doc.CreateElement(“OrderItem”);
orderItem1.SetAttribute(“Name”, “电脑”);
orderItem1.SetAttribute(“Count”, “30”);
Items.AppendChild(orderItem1);

XmlElement orderItem2 = doc.CreateElement(“OrderItem”);
orderItem2.SetAttribute(“Name”, “电视”);
orderItem2.SetAttribute(“Count”, “1”);
Items.AppendChild(orderItem2);
doc.Save(“c:\2.xml”);

}

/// <summary>
/// 读取xml
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
//<Books> /// <Books>
// <Book>
// <Name>红楼梦</Name>
// <Price>10</Price>
// </Book>
// <Book>
// <Name>西游记</Name>
// <Price>20</Price>
// </Book>
//</Books>
protected void Unnamed1_Click(object sender, EventArgs e)
{
string path = “c:\1.xml”;
XmlDocument doc = new XmlDocument();
doc.Load(path);
//根节点 BOOKs
XmlNode parent = doc.DocumentElement;

XmlNodeList xnl = parent.ChildNodes; ;
foreach (XmlNode node in xnl)
{

//Book
Response.Write(node.ChildNodes[0].InnerText); //后楼梦
Response.Write(node.ChildNodes[1].InnerText); //10

}

}

//读取带属性的xml
protected void t1_Click(object sender, EventArgs e)
{

// <Order>
// <CustomerName>n1</CustomerName>
// <OrderNumber>BJ200088</OrderNumber>
// <Items>
// <OrderItem Name=”电脑” Count=”30″ />
// <OrderItem Name=”电视” Count=”1″ />
// </Items>
//</Order>
//string path = “c:\2.xml”;
//XmlDocument doc = new XmlDocument();
//doc.Load(path);

//父节点 Order
//XmlElement parent = doc.DocumentElement;
//XmlNode items = parent.SelectSingleNode(“Items”);
//foreach (XmlNode item in items.ChildNodes)
//{
// Response.Write(item.Attributes[“Name”].Value);
// Response.Write(item.Attributes[“Count”].Value);

//}

//2使用 xpath 方式读取
//string path = “c:\2.xml”;
//XmlDocument doc = new XmlDocument();
//doc.Load(path);

//XmlNodeList xnl = doc.SelectNodes(“Order/Items/OrderItem”);
//foreach (XmlNode item in xnl)
//{

// Response.Write(item.Attributes[0].Value);
// Response.Write(item.Attributes[2].Value);
//}

// 3 也是使用xpath 但是定位不一样
//string path = “c:\2.xml”;
//XmlDocument doc = new XmlDocument();
//doc.Load(path);
//XmlNode node= doc.SelectSingleNode(“Order/Items/OrderItem[@Name=”电脑”]”);
// Response.Write(node.Attributes[1].Value); //30
// //node.Attributes[“Count”] = 40;
// //doc.Save(path);

//更新 30

//4
// string path = “c:\2.xml”;
// XmlDocument doc = new XmlDocument();
// doc.Load(path);
// XmlNode node= doc.SelectSingleNode(“Order/Items”);
// Response.Write(node.InnerXml);
//// Response.Write(node.InnerText);

// 操作xml文件
// XmlDocument
// Save()
// Load()
// SelectSingleNode() 根据xpath找某个节点
// SelectNodes()
// DocumentElement 根节点

// XmlNode
// XmlElement
// SetAttribute(); 设置属性
// Attributes[].Value 读取属性
// InnerText
// InnerXml
// ChildNodes

//委托:类型安全的指向函数的指针
//使用步骤
// 1:声明一个委托 delegate string DelString(string s) 2:定义一个委托变量
// DelString del = new DelString(ToUpper)
// DelString del = ToUpper
// 3:使用委托
// del(s);

}

}
}

《Xml基础》有4个想法
  1. Wow, marvelous weblog format! How lengthy have
    you been blogging for? you made running a blog look easy.
    The overall glance of your web site is fantastic, let alone the content
    material! You can see similar here sklep internetowy

  2. An intriguing discussion is definitely worth comment. There’s
    no doubt that that you ought to publish more on this subject, it
    might not be a taboo subject but generally people don’t speak about such topics.
    To the next! All the best!! I saw similar here: Sklep online

  3. Hi! Do you know if they make any plugins to help with Search Engine Optimization? I’m
    trying to get my blog to rank for some targeted keywords but I’m not seeing very
    good success. If you know of any please share. Appreciate it!
    You can read similar text here: Sklep internetowy

  4. Good day! Do you know if they make any plugins
    to assist with SEO? I’m trying to get my site to rank for some targeted keywords
    but I’m not seeing very good gains. If you know of any please
    share. Appreciate it! You can read similar text
    here: GSA Verified List

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注