Xml Tips
Xml Tips
//z 2012-3-7 16:43:47 PM IS2120@CSDN
1. xml 中的注釋
<!-- 這是注釋 -->
2. 驗證 xml 的合法性
??感覺不合適
public class MyXmlDocument: XmlDocument {bool TryParseXml(string xml){try{ParseXml(xml);return true;}catch(XmlException e){return false;}} Using a XmlValidatingReader will prevent the exceptions, if you provide your own ValidationEventHandler.
using?System;
using?System.Xml;
using?System.Xml.Schema;
using?System.Xml.XPath;
class?XPathValidation
{
????static?void?Main()
????{
????????try
????????{
????????????XmlReaderSettings settings =?new?XmlReaderSettings();
????????????settings.Schemas.Add("http://www.contoso.com/books",?"contosoBooks.xsd");
????????????settings.ValidationType = ValidationType.Schema;
????????????XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);
????????????XmlDocument document =?new?XmlDocument();
????????????document.Load(reader);
????????????ValidationEventHandler eventHandler =?new?ValidationEventHandler(ValidationEventHandler);
????????????// the following call to Validate succeeds.
????????????document.Validate(eventHandler);
????????????// add a node so that the document is no longer valid
????????????XPathNavigator navigator = document.CreateNavigator();
????????????navigator.MoveToFollowing("price",?"http://www.contoso.com/books");
????????????XmlWriter writer = navigator.InsertAfter();
????????????writer.WriteStartElement("anotherNode",?"http://www.contoso.com/books");
????????????writer.WriteEndElement();
????????????writer.Close();
????????????// the document will now fail to successfully validate
????????????document.Validate(eventHandler);
????????}
????????catch?(Exception ex)
????????{
????????????Console.WriteLine(ex.Message);
????????}
????}
????static?void?ValidationEventHandler(object?sender, ValidationEventArgs e)
????{
????????switch?(e.Severity)
????????{
????????????case?XmlSeverityType.Error:
????????????????Console.WriteLine("Error: {0}", e.Message);
????????????????break;
????????????case?XmlSeverityType.Warning:
????????????????Console.WriteLine("Warning {0}", e.Message);
????????????????break;
????????}
????}
}
//z 2012-3-7 16:43:47 PM IS2120@CSDN
1. xml 中的注釋
<!-- 這是注釋 -->
并非用于 XML 分析器的內(nèi)容(例如與文檔結(jié)構(gòu)或編輯有關(guān)的說明)可以包含在注釋中。注釋以 <!-- 開頭,以 --> 結(jié)尾,例如<!--catalog last updated 2000-11-01-->。
注釋可以出現(xiàn)在文檔序言中,包括文檔類型定義 (DTD);文檔之后;或文本內(nèi)容中。注釋不能出現(xiàn)在屬性值中。不能出現(xiàn)在標記中。
分析器在遇到 > 時,就認為注釋已結(jié)束;然后繼續(xù)將文檔作為正常的 XML 處理。因此,字符串 > 不能出現(xiàn)在注釋中。除了該限制之外,任何合法的 XML 字符均可以出現(xiàn)在注釋中,與 CDATA 節(jié)非常類似。這樣,可以從分析器看到的輸出流中刪除 XML 注釋,同時又不會刪除文檔的內(nèi)容。
以下注釋可以用于暫時除去標記。
<!--- <test pattern="SECAM" /><test pattern="NTSC" /> -->| 在 HTML 中,可以使用注釋來隱藏腳本和樣式表。要在 XML 中使用此方法,可能必須檢索注釋,提取注釋的內(nèi)容,檢查是否有標記字符,然后再進行重新分析。在此例中,CDATA 節(jié)是更好的選擇。 |
2. 驗證 xml 的合法性
??感覺不合適
public class MyXmlDocument: XmlDocument {bool TryParseXml(string xml){try{ParseXml(xml);return true;}catch(XmlException e){return false;}} Using a XmlValidatingReader will prevent the exceptions, if you provide your own ValidationEventHandler.
using?System;
using?System.Xml;
using?System.Xml.Schema;
using?System.Xml.XPath;
class?XPathValidation
{
????static?void?Main()
????{
????????try
????????{
????????????XmlReaderSettings settings =?new?XmlReaderSettings();
????????????settings.Schemas.Add("http://www.contoso.com/books",?"contosoBooks.xsd");
????????????settings.ValidationType = ValidationType.Schema;
????????????XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);
????????????XmlDocument document =?new?XmlDocument();
????????????document.Load(reader);
????????????ValidationEventHandler eventHandler =?new?ValidationEventHandler(ValidationEventHandler);
????????????// the following call to Validate succeeds.
????????????document.Validate(eventHandler);
????????????// add a node so that the document is no longer valid
????????????XPathNavigator navigator = document.CreateNavigator();
????????????navigator.MoveToFollowing("price",?"http://www.contoso.com/books");
????????????XmlWriter writer = navigator.InsertAfter();
????????????writer.WriteStartElement("anotherNode",?"http://www.contoso.com/books");
????????????writer.WriteEndElement();
????????????writer.Close();
????????????// the document will now fail to successfully validate
????????????document.Validate(eventHandler);
????????}
????????catch?(Exception ex)
????????{
????????????Console.WriteLine(ex.Message);
????????}
????}
????static?void?ValidationEventHandler(object?sender, ValidationEventArgs e)
????{
????????switch?(e.Severity)
????????{
????????????case?XmlSeverityType.Error:
????????????????Console.WriteLine("Error: {0}", e.Message);
????????????????break;
????????????case?XmlSeverityType.Warning:
????????????????Console.WriteLine("Warning {0}", e.Message);
????????????????break;
????????}
????}
}
轉(zhuǎn)載于:https://www.cnblogs.com/IS2120/archive/2012/03/07/6745931.html
總結(jié)
- 上一篇: C++.Templates学习总结归纳1
- 下一篇: 使用DIV之后 table何去何从