C# XML - XmlDocument

http://msdn.microsoft.com/zh-cn/library/System.Xml.XmlDocument(v=vs.80).aspx

 

1.member

http://msdn.microsoft.com/zh-cn/library/system.xml.xmldocument_members(v=vs.80).aspx

 名称                              说明              
Public property Supported by the .NET Compact Framework Attributes  获取一个 XmlAttributeCollection,它包含该节点的属性。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework BaseURI 已重写。获取当前节点的基 URI。
Public property Supported by the .NET Compact Framework ChildNodes  获取节点的所有子节点。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework DocumentElement 获取文档的根 XmlElement
Public property DocumentType 获取包含 DOCTYPE 声明的节点。
Public property Supported by the .NET Compact Framework FirstChild  获取节点的第一个子级。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework HasChildNodes  获取一个值,该值指示节点是否有任何子节点。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework Implementation 获取当前文档的 XmlImplementation 对象。
Public property Supported by the .NET Compact Framework InnerText  获取或设置节点及其所有子节点的串联值。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework InnerXml 已重写。获取或设置表示当前节点子级的标记。
Public property Supported by the .NET Compact Framework IsReadOnly 已重写。获取一个值,该值指示当前节点是否是只读的。
Public property Supported by the .NET Compact Framework Item  已重载。获取指定的子元素。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework LastChild  获取节点的最后一个子级。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework LocalName 已重写。获取节点的本地名称。
Public property Supported by the .NET Compact Framework Name 已重写。获取节点的限定名。
Public property Supported by the .NET Compact Framework NamespaceURI  获取该节点的命名空间 URI。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework NameTable 获取与此实现关联的 XmlNameTable
Public property Supported by the .NET Compact Framework NextSibling  获取紧接在该节点之后的节点。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework NodeType 已重写。获取当前节点的类型。
Public property Supported by the .NET Compact Framework OuterXml  获取表示此节点及其所有子节点的标记。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework OwnerDocument 已重写。获取当前节点所属的 XmlDocument
Public property Supported by the .NET Compact Framework ParentNode 已重写。获取该节点(对于可以具有父级的节点)的父级。
Public property Supported by the .NET Compact Framework Prefix  获取或设置该节点的命名空间前缀。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework PreserveWhitespace 获取或设置一个值,该值指示是否在元素内容中保留空白。
Public property Supported by the .NET Compact Framework PreviousSibling  获取紧接在该节点之前的节点。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework SchemaInfo 已重写。返回节点的后架构验证信息集 (PSVI)。
Public property Supported by the .NET Compact Framework Schemas 获取或设置与此 XmlDocument 关联的 XmlSchemaSet 对象。
Public property Supported by the .NET Compact Framework Value  获取或设置节点的值。(从 XmlNode 继承。)
Public property Supported by the .NET Compact Framework XmlResolver 设置 XmlResolver 以用于解析外部资源。

 2. function

名称                              说明              
Public method Supported by the .NET Compact Framework AppendChild  将指定的节点添加到该节点的子节点列表的末尾。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework Clone  创建此节点的一个副本。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework CloneNode 已重写。 创建此节点的一个副本。
Public method Supported by the .NET Compact Framework CreateAttribute 已重载。 创建具有指定名称的 XmlAttribute
Public method Supported by the .NET Compact Framework CreateCDataSection 创建包含指定数据的 XmlCDataSection
Public method Supported by the .NET Compact Framework CreateComment 创建包含指定数据的 XmlComment
Public method Supported by the .NET Compact Framework CreateDocumentFragment 创建 XmlDocumentFragment
Public method CreateDocumentType 返回新的 XmlDocumentType 对象。
Public method Supported by the .NET Compact Framework CreateElement 已重载。 创建 XmlElement
Public method Supported by the .NET Compact Framework CreateEntityReference 创建具有指定名称的 XmlEntityReference
Public method CreateNavigator 已重载。                已重写。 创建一个用于导航此文档的新 XPathNavigator 对象。
Public method Supported by the .NET Compact Framework CreateNode 已重载。 创建 XmlNode
Public method Supported by the .NET Compact Framework CreateProcessingInstruction 创建一个具有指定名称和数据的 XmlProcessingInstruction
Public method Supported by the .NET Compact Framework CreateSignificantWhitespace 创建一个 XmlSignificantWhitespace 节点。
Public method Supported by the .NET Compact Framework CreateTextNode 创建具有指定文本的 XmlText
Public method Supported by the .NET Compact Framework CreateWhitespace 创建一个 XmlWhitespace 节点。
Public method Supported by the .NET Compact Framework CreateXmlDeclaration 创建一个具有指定值的 XmlDeclaration 节点。
Public method Supported by the .NET Compact Framework Equals  已重载。 确定两个 Object 实例是否相等。 (从 Object 继承。)
Public method GetElementById 获取具有指定 ID 的 XmlElement
Public method Supported by the .NET Compact Framework GetElementsByTagName 已重载。 返回一个 XmlNodeList,它包含与指定名称匹配的所有子代元素的列表。
Public method Supported by the .NET Compact Framework GetEnumerator  提供对 XmlNode 中节点上“for each”样式迭代的支持。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework GetHashCode  用作特定类型的哈希函数。GetHashCode 适合在哈希算法和数据结构(如哈希表)中使用。 (从 Object 继承。)
Public method Supported by the .NET Compact Framework GetNamespaceOfPrefix  查找当前节点范围内离给定的前缀最近的 xmlns 声明,并返回声明中的命名空间 URI。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework GetPrefixOfNamespace  查找当前节点范围内离给定的命名空间 URI 最近的 xmlns 声明,并返回声明中定义的前缀。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework GetType  获取当前实例的 Type。 (从 Object 继承。)
Public method Supported by the .NET Compact Framework ImportNode 将节点从另一个文档导入到当前文档。
Public method Supported by the .NET Compact Framework InsertAfter  将指定的节点紧接着插入指定的引用节点之后。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework InsertBefore  将指定的节点紧接着插入指定的引用节点之前。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework Load 已重载。 加载指定的 XML 数据。
Public method Supported by the .NET Compact Framework LoadXml 从指定的字符串加载 XML 文档。
Public method Supported by the .NET Compact Framework Normalize  将此 XmlNode 下子树完全深度中的所有 XmlText 节点都转换成“正常”形式,在这种形式中只有标记(即标记、注释、处理指令、CDATA 节和实体引用)分隔 XmlText 节点,也就是说,没有相邻的 XmlText 节点。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework PrependChild  将指定的节点添加到该节点的子节点列表的开头。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework ReadNode 根据 XmlReader 中的信息创建一个 XmlNode 对象。读取器必须定位在节点或属性上。
Public method Static Supported by the .NET Compact Framework ReferenceEquals  确定指定的 Object 实例是否是相同的实例。 (从 Object 继承。)
Public method Supported by the .NET Compact Framework RemoveAll  移除当前节点的所有子节点和/或属性。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework RemoveChild  移除指定的子节点。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework ReplaceChild  newChild 节点替换子节点 oldChild。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework Save 已重载。 将 XML 文档保存到指定的位置。
Public method Supported by the .NET Compact Framework SelectNodes  已重载。 选择匹配 XPath 表达式的节点列表。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework SelectSingleNode  已重载。 选择匹配 XPath 表达式的第一个 XmlNode。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework Supports  测试 DOM 实现是否实现特定的功能。 (从 XmlNode 继承。)
Public method Supported by the .NET Compact Framework ToString  返回表示当前 ObjectString。 (从 Object 继承。)
Public method Supported by the .NET Compact Framework Validate 已重载。 验证 XmlDocument 是不是 Schemas 属性中包含的 XML 架构定义语言 (XSD) 架构。
Public method Supported by the .NET Compact Framework WriteContentTo 已重写。 将 XmlDocument 节点的所有子级保存到指定的 XmlWriter 中。
Public method Supported by the .NET Compact Framework WriteTo 已重写。 将 XmlDocument 节点保存到指定的 XmlWriter

 3. example

1         private void button1_Click(object sender, EventArgs e)
2         {
3              XmlDocument doc = new XmlDocument();
4              doc.Load(@"..\..\CD.xml");
5 
6              XmlElement root = null;
7              root = doc.DocumentElement;
8 
9 XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
10             nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
11            
12             XmlNodeList listNodes = null;
13             listNodes = doc.SelectNodes("/ab:bookstore/ab:book[ab:author[ab:sex]]/ab:price", nsmgr);
14             foreach (XmlNode node in listNodes )
15             {
16                 richTextBox1.Text += node.InnerText + "\n";
17             }
18         }


 

example 2 - insert node

 1 XmlDocument xmlDoc=new XmlDocument();
 2 xmlDoc.Load("bookstore.xml");
 3 XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
 4 XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
 5 xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
 6 xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
 7 
 8 XmlElement xesub1=xmlDoc.CreateElement("title");
 9 xesub1.InnerText="CS从入门到精通";//设置文本节点
10 xe1.AppendChild(xesub1);//添加到<book>节点中
11 XmlElement xesub2=xmlDoc.CreateElement("author");
12 xesub2.InnerText="候捷";
13 xe1.AppendChild(xesub2);
14 XmlElement xesub3=xmlDoc.CreateElement("price");
15 xesub3.InnerText="58.3";
16 xe1.AppendChild(xesub3);
17 
18 root.AppendChild(xe1);//添加到<bookstore>节点中
19 xmlDoc.Save("bookstore.xml");

 

example 3- modify

 1 XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
 2 foreach(XmlNode xn in nodeList)//遍历所有子节点
 3 {
 4     XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
 5     if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
 6     {
 7         xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”
 8 
 9         XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
10          foreach(XmlNode xn1 in nls)//遍历
11          {
12               XmlElement xe2=(XmlElement)xn1;//转换类型
13               if(xe2.Name=="author")//如果找到
14               {
15                    xe2.InnerText="亚胜";//则修改
16                    break;//找到退出来就可以了
17               }
18          }
19          break;
20    }
21 }
22 
23 xmlDoc.Save("bookstore.xml");//保存。

 

 

posted @ 2014-06-05 11:11  vivianC  阅读(355)  评论(0编辑  收藏  举报