C# Xml基礎知識

XML(EXtensible Markup Language)是一種平臺無關的,被設計用來傳輸和存儲數據。函數

  Root:根節點,有且只能有一個;spa

  Element:節點元素;設計

  Attribute:節點屬性;code

  Content:內容(非空白文本、CDATA、Element、EndElement、EntityReference 或 EndEntity)節點。orm

<?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 Xmlxml

  XmlTextWrite和XmlDocument哪一個比較好?blog

  若是要寫入Xml流的數據已經準備好,優先選擇XmlTextWriter類,可是若是是創建Xml文檔的一小部分,在不一樣的地方插入節點,用XmlDocument建立文檔就比較好。繼承

 

流讀寫

XmlTextWriter的經常使用函數

1. WriteStartDocument()與WriteEndDocument()

  用於寫Xml頭聲明,例如:<?xml version="1.0" encoding="utf-8">。 ip

  必須在編寫結束時調用 WriteEndDocument(),以結束寫過程,它會清空XmlTextWriter中維護的全部堆棧和臨時信息,相似於Dispose()函數。 utf-8

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

相關文章
相關標籤/搜索