C# Xml基础知识

XML(EXtensible Markup Language)是一种平台无关的,被设计用来传输和存储数据。

  Root:根节点,有且只能有一个;

  Element:节点元素;

  Attribute:节点属性;

  Content:内容(非空白文本、CDATA、Element、EndElement、EntityReference 或 EndEntity)节点。

<?xml version='1.0'?> <!--  XmlDeclaration -->
<root>
<element attName=attValue>
content
</element>
</root>
<!-- my comment -->

常用技术

  • XmlTextWriter和XmlTextReader分别继承于XmlWriter和XmlReader,属于流读写的方式。
  • XmlDocument,属于DOM (Document Object Model)。
  • Linq to Xml

  XmlTextWrite和XmlDocument哪个比较好?

  如果要写入Xml流的数据已经准备好,优先选择XmlTextWriter类,但是如果是建立Xml文档的一小部分,在不同的地方插入节点,用XmlDocument创建文档就比较好。

流读写

XmlTextWriter的常用函数

1. WriteStartDocument()与WriteEndDocument()

  用于写Xml头声明,例如:<?xml version="1.0" encoding="utf-8">。 

  必须在编写结束时调用 WriteEndDocument(),以结束写过程,它会清空XmlTextWriter中维护的所有堆栈和临时信息,类似于Dispose()函数。 

2. WriteStartElement()与WriteEndElement()

  创建当前节点的一个子节点。

  通常使用WriteStartElement(string LocalName,string Value),其中LocalName表示节点名称,value表示节点的InnerText。生成的xml:<LocalName>Value</LocalName> 

  而一旦使用过一句WriteStartElement()就必须有对应的WriteEndElement(),将当前指向的节点就转为父节点,如: <ParentName><LocalName>Value</LocalName></ParentName> 

3. WriteElementString(string LocalName, string Value)

  与WriteAttributeString的用法相同,其中LocalName为节点名称,Value为节点的InnerText。

4. WriteStartAttribute()与WriteEndAttribute()

  与节点创建函数用法相同 

5. WriteAttributeString(string LocalName,string value) 

  创建属性,不需要使用WriteEndAttribute()。

  WriteAttributeString生成的是未处理的原始语句,而WriteStartAttribute()生成的是Xml Schema语句。  

//使用WriteStartAttribute与WriteEndAttribute
WriteStartElement("address");  
WriteStartAttribute("state","California"); 
WriteEndAttribute(); 
WriteEndElement();
//生成的xml:<address d1p1:state="" xmlns:d1p1="California" /> 
//使用WriteAttributeString
WriteStartElement("address");
WriteAttributeString("state","California"); 
WriteEndElement();
//生成的xml: <address state="California" />

6. WriteString()与WriteRaw()的区别

  WriteString会把敏感字符转换为转义字符;

  而WriteRaw()则是直接写入,不做任何处理。

WriteString("More >")
//生成的Xml是More &gt

WriteRaw("More >")
//生成的xml是More >   

7. WriteCData()

  用来写用CData包裹的字符串,在字符串中有敏感字符时很有用。  

WriteCData("More >")
//生成的Xml是<![CDATA [More >]] >

8.WriteFullEndElement()

//该函数用来写完整的结束标志,如<address></address>,
writer.WriteStartElement("address", null);
writer.WriteFullEndElement();

XmlTextReader的常用函数

1. Read

  读取一个节点。

2. NodeType

  获取当前节点的类型,如:xml.NodeType == XmlNodeType.Element

3. GetAttribute

  获取属性的值。

DOM

  XmlDocument创建Xml文件的类  

private XmlDocument doc= new XmlDocument();
private void button2_Click(object sender, System.EventArgs e)
{
     XmlDeclaration newDec = doc.CreateXmlDeclaration("1.0",null,null);
     doc.AppendChild(newDec);
     XmlElement newRoot = doc.CreateElement("newBookstore");
     doc.AppendChild(newRoot);

     //创建一个新的book元素
     XmlElement newBook = doc.CreateElement("book");
     //创建并设置book元素的属性
     newBook.SetAttribute("genre","Mystery");
     newBook.SetAttribute("publicationdate","2001");
     newBook.SetAttribute("ISBN","123456789");
     //创建一个title元素
     XmlElement newTilte = doc.CreateElement("title");
     newTilte.InnerText  ="Case of the Missing Cookie";
     newBook.AppendChild(newTilte);
     //创建author元素
     XmlElement newAuthor = doc.CreateElement("author");
     newBook.AppendChild(newAuthor);

     XmlElement newName = doc.CreateElement("name");
     newName.InnerText  = "C.Monster";
     newAuthor.AppendChild(newName);

     XmlElement newPrice = doc.CreateElement("price");
     newPrice.InnerText = "9.95";
     newBook.AppendChild(newPrice);
     doc.DocumentElement.AppendChild(newBook);
     XmlTextWriter tr = new XmlTextWriter("booksEdit.xml",null);
     tr.Formatting = Formatting.Indented;
     doc.WriteContentTo(tr);
     tr.Close();
}

//代码生成后的文档:
//<?xml version="1.0"?>
//<newBookstore>
//    <book genre="Mystery" publicationdate="2001" ISBN="123456789">
//        <title>Case of the Missing Cookie</title>
//        <author>
//            <name>C.Monster</name>
//        </author>
//        <price>9.95</price>
//    </book>
//</newBookstore>    

Linq to Xml

https://msdn.microsoft.com/en-us/library/bb387098(v=vs.100).aspx

资料

XML 新手入门基础知识:https://www.ibm.com/developerworks/cn/xml/x-newxml/

XML @ Wikipedia:https://en.wikipedia.org/wiki/XML

原文地址:https://www.cnblogs.com/luciakally/p/6387787.html