xml

it2022-05-05  121

1:什么是XML? 可扩展标记性语言,用于描述数据 2:【代码】输入以下xml格式,并生成bookstore.xml文件

<?xml version="1.0" encoding="utf-8"?> 数据结构 严蔚敏 30.00 static void Main(string[] args) { XmlDocument xmlDocument = new XmlDocument(); XmlDeclaration declaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null); xmlDocument.AppendChild(declaration); XmlElement stu = xmlDocument.CreateElement("bookstore"); xmlDocument.AppendChild(stu); XmlElement title = xmlDocument.CreateElement("title"); title.InnerText="数据结构"; XmlElement author = xmlDocument.CreateElement("author"); author.InnerText = "严蔚敏"; XmlElement price = xmlDocument.CreateElement("price"); price.InnerText = "30.00"; stu.AppendChild(title); stu.AppendChild(author); stu.AppendChild(price); xmlDocument.Save("E:\\新建文本文档.xml"); } 3:创建XML文档对象的类, 创建XML头的类,创建XML节点的类分别是哪个? 创建XML文档对象 XmlDocument 创建XML头的类 XmlDeclaration创建XML节点的类 XmlElement 4;节点添加方法,保存XML方法,加载XML方法,读取XML节点方法分别是 节点添加 AppendChild() 保存XML Save() 加载XML Load() 读取XML节点 XmlNode node = doc.SelectSingleNode("xxx"); foreach (XmlNode item in node.ChildNodes) { } XmlNode node = doc.SelectSingleNode("xxx");//获取指定二层节点 5; 读取节点的值,读取节点属性的值? XmlNode node = doc.SelectSingleNode("xxx");//获取指定二层节点 foreach (XmlNode item in node.ChildNodes) //在获取三层节点 { book.Type = item.Attributes["Type"].Value;//读取节点属性的值 book.ISBN = item.Attributes["ISBN"].Value;//读取节点属性的值 book.title = item["title"].InnerText; //读取节点的值 book.author = item["author"].InnerText; //读取节点的值 }

06:将以下格式

<?xml version="1.0" encoding="utf-8"?> 数据结构 严蔚敏 30.00 算法 严蔚敏 10.00 转换成类 BookStore 有以下属性:List books; Book类有以下属性: Type,ISBN,title,author,price BookStore store = new BookStore(); store.books = new List(); XmlDocument doc = new XmlDocument(); doc.Load(@"D:\\str.xml"); XmlNode node = doc.SelectSingleNode("bookstore"); foreach (XmlNode item in node.ChildNodes) { Book book = new Book(); book.Type = item.Attributes["Type"].Value; book.ISBN = item.Attributes["ISBN"].Value; book.title = item["title"].InnerText; book.author = item["author"].InnerText; book.price = Convert.ToDouble(item["price"].InnerText); store.books.Add(book); } foreach (Book item in store.books) { Console.WriteLine(item.Type+"\t"+item.ISBN+"\t"+item.title+"\t"+item.price+"\t"+item.author);

最新回复(0)