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

(C#)xml的简单理解创建和读取

2015-11-11 20:28 537 查看
xml知识点清理:一、文档规则1.区分大小写。2.属性值必须加引号(单引号、双引号都可以),一般情况下建议使用使用双引号。3.所有标记必须有结束符号。4.所有空标记必须关闭。5.必须有且仅有一根元素。6.解析空白字符时,会按照实际内容输出,不会缩略。7.标记名称约定:a.可以包含字母、数字、其他字符。b.不能以数字和下划线开头。c.不能以xml或者Xml之类的字符开头。d.不能包含空格二、声明xml声明标准语句:<?xml version="1.0" encoding="GB2312" standalone="yes"?>强调:1.“<?xml” 中的"<?"和"xml"中间不能有空格。2.”?>"之前可以有空格,也可以没有。3.enconding和standalone是可选属性,encoding默认值是"UTF-8",standalone默认值是"no"。4.常见的编码方式有:简体中文:GB2312繁体中文 :BIG5西欧字符:UTF-8,UTF-165.standalone表示文档是否附带DTD文件。三、xml属性因为数据既可以存储在子元素中也可以存储在属性中,那么何时用属性,何时用子元素,没有固定规则,但是建议:元数据应该以属性的方式存储,而数据本身应该以元素的形式存储。使用属性会引发以下问题:1.属性不能包含多个值(子元素可以)。2.属性不容易扩展。3.属性不能描述结构(子元素可以)。4.属性很难通过DTD进行测试。四、命名空间声明命名空间俩种方式:1.默认声明,所有元素不需要指定前缀,如:<schema xmlns="http://www.w3.org/2001/XMLschema"><element name="diguonianzhu" type="string"/>……</schema>2.明确声明,xmlns关键字与一个命名空间的URI的前缀相关联,所有元素都需要指定前缀,如:<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLschema"><xsd:element name="diguonianzhu" type="string"/>     ……<xsd:/schema>强调:用来表示命名空间的URI并不被XML解析器调用,实际上并不去访问这个URI,它只是表示一个标识名字而已。五、XML的语法-> XML文件,是一个纯文本文件-> XML结构是一个树状结构-> 文档描述-> 根元素-> 数据存储在标签元素中-> 标签时具有属性六、 验证XML写的是否正确,使用浏览器将其打开七、 XML元素的标签名可以随意的命名(和html)不能使用数字开头不能包含空格保存数据,将具体数据放在标签中,让标签组合在一起表示一个具体的内容.常常使用属性标记一些标签,属性一定是键值对,并且使用双引号将值括起来8、 使用<!-- 表示注释 -->八、 大小写敏感的九、使用C#操作XML-> DOM,Document Object Model 文档对象模型-> 将文档看做成对象,而问但中的节点也是对象节点中的节点也是对象-> 使用DOM操作,常用的类XmlDocument 文档XmlElement 元素XmlAttribute 属性XmlText 文本-> 比较好的理解dom11、Linq to XML-> XDocument-> XElement-> XAttribute-> XName1.理解程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Xml;

namespace CreateXML
{
class Program
{
static void Main(string[] args)
{
// 1.使用XMLDocument创建文档
XmlDocument myXml = new XmlDocument();
/*
使用myXml对象的create系方法创建需要的节点
*/
// 2.创建文档描述
XmlDeclaration xd = myXml.CreateXmlDeclaration("1.0","utf-8",null);
// 使用父节点.AppendChild(子节点)
myXml.AppendChild(xd);
// 3.创建根结点
XmlElement xmlRoot = myXml.CreateElement("personNode");
myXml.AppendChild(xmlRoot);
// DOM将所有的内容都看成对象,所以加载节点,就是在创建对象,并且将对象加到节点上
// 接下来创建内容(创建,追加)

// 4.创建 person
XmlElement person = myXml.CreateElement("person");
// 5.追加
xmlRoot.AppendChild(person);

// 6.在person下面追加,name age sex
XmlElement name = myXml.CreateElement("name");
person.AppendChild(name);

XmlElement age = myXml.CreateElement("age");
person.AppendChild(age);

XmlElement sex = myXml.CreateElement("sex");
person.AppendChild(sex);

// 7.追加ID
XmlAttribute xid = myXml.CreateAttribute("id");
// 为 id 赋值,既然是对象,值和属性有关
xid.Value = "001";
// person.SetAttribute("id","001");
person.SetAttributeNode(xid);

// 8.在 name age sex 中追加数据
XmlText xname = myXml.CreateTextNode("静水深流");
name.AppendChild(xname);
XmlText xage = myXml.CreateTextNode("18");
age.AppendChild(xage);
XmlText xsex = myXml.CreateTextNode("男");
sex.AppendChild(xsex);
// 最后用Save方法保存文档
myXml.Save("Persons.xml");
}
}
}
  2.理解Dom结构:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml.Linq;namespace UseLinkToXmlToCreateXml{class Program{static void Main(string[] args){XDocument xdoc = new XDocument();// 创建根元素 追加数据XElement root = new XElement("root");xdoc.Add(root);XElement person = new XElement("person");XAttribute xId = new XAttribute("id","001");XElement name = new XElement("name");XElement age = new XElement("age");XElement sex = new XElement("sex");// 给对象赋值name.Value = "静水深流";sex.Value = "男";age.Value = "18";person.Add(xId,name,age,sex);root.Add(person);xdoc.Save("LinkToXML.xml");}}}
  3.C#创建XML文档:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml.Linq;namespace FinalCreateXml{class Program{static void Main(string[] args){#region 一个代码段节点//new XDocument(new XElement("root",//       new XElement("person",//               new XAttribute("id", "001"),//               new XElement("name", "静水深流"),//               new XElement("age", 18),//               new XElement("sex", "男"))//   )).Save("per.xml");#endregionRandom rd = new Random();XDocument xdoc = new XDocument(new XElement("personCollection"));// 从文本文件或数据库中读取文件#region 多个代码节点//for (int i = 0; i < 100; i++)//{//    xdoc.Root.Add(//            new XElement("person",//                    new XAttribute("id", i + 1),//                    new XElement("name", "静水深流" + 1),//                    new XElement("age", rd.Next(24)),//                    new XElement("sex", "男女"[rd.Next(2)])//                )//        );//}//xdoc.Save("100Persons.xml");#endregion#region stringToXMLXDocument.Parse(@"<root><person id=""001"">静水深流</person></root>").Save("stringToXML.xml");#endregion}}}
  4.读取XML文档:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml.Linq;namespace ReadXML{class Program{static void Main(string[] args){string path = "100Persons.xml";// 加载xml文件XDocument xdoc = XDocument.Load(path);// 查找// e 代表的就是节点对象//var query = xdoc.DescendantNodes().Where(e=>{返回bool值的判断条件});var query = xdoc.DescendantNodes().Where(e=>{//返回bool值的判断条件XElement ele = e as XElement;if (ele == null) return false;XElement age = ele.Element("age");XElement sex = ele.Element("sex");if (age != null && sex != null){int numAge = Convert.ToInt32(age.Value);if (numAge >= 15 && numAge <= 18 && sex.Value == "男"){return true;}else{return false;}}else{return false;}});XElement root = new XElement("root");foreach (XElement item in query){root.Add(item);}new XDocument(root).Save("Search.xml");}}}
  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: