您的位置:首页 > 职场人生

.NET平台开发必须掌握的XML知识(一)

2007-02-21 00:59 363 查看
using System;
using System.IO;
using System.Xml;

//XmlNode.NextSibling 属性 作用:获取紧接在该节点之后的节点。属性值:下一个 XmlNode。如果没有下一个节点,则返回 空引用(在 Visual Basic 中为 Nothing)。
//public virtual XmlNode FirstChild { get; }
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XMLFile1.xml");//把XML文档装入内存

//XmlDocument.DocumentElement属性返回一个XmlElement类的实例,表示XmlElement的根节点
XmlNode currNode = doc.DocumentElement.FirstChild;//public virtual XmlNode FirstChild { get; }
Console.WriteLine("First book..."); //该属性返回一个XmlNode对象(或者实例)
Console.WriteLine(currNode.OuterXml);
XmlNode nextNode = currNode.NextSibling; //XmlNode nextNode = doc.DocumentElement.NextSibling;这样写会产生异常
Console.WriteLine("\r\nSecond book...");
Console.WriteLine(nextNode.OuterXml);
Console.WriteLine();
Console.WriteLine(doc.DocumentElement.OuterXml);//根也是节点,所以也会把根节点也打印出来
Console.WriteLine();
Console.WriteLine(doc.OuterXml);//说明doc对象相当于整个XML文档,所以此时会全部打印出来
Console.WriteLine();
Console.WriteLine(doc.FirstChild.OuterXml); //因为doc对象相当于整个XML文档
Console.WriteLine();
Console.WriteLine(doc.LastChild.OuterXml); //因为doc对象相当于整个XML文档
}
}

//XmlDocument.DocumentElement 属性 作用:获取文档的根 XmlElement。属性值:表示 XML 文档树的根的 XmlElement。如果不存在根,则返回 空引用(在 Visual Basic 中为 Nothing)。
//public XmlElement DocumentElement { get; }
//public class Sample
//{
// public static void Main()
// {
// //Create the XmlDocument.
// XmlDocument doc = new XmlDocument();
// //用指定的字符串加载XML文档
// doc.LoadXml("<?xml version='1.0' ?>" +
// "<book genre='novel' ISBN='1-861001-57-5'>" +
// "<title>Pride And Prejudice</title>" +
// "</book>");
// //Display the document element.
// Console.WriteLine(doc.DocumentElement.OuterXml);
// }
//}

//XmlNode.FirstChild 属性 作用:获取节点的第一个子级。属性值:节点的第一个子级。如果没有这样的节点,则返回 空引用(在 Visual Basic 中为 Nothing)。
//public virtual XmlNode FirstChild { get; }
//public class Sample
//{
// public static void Main()
// {
// XmlDocument doc = new XmlDocument();
// doc.LoadXml("<book ISBN='1-861001-57-5'>" +
// "<title>Pride And Prejudice</title>" +
// "<price>19.95</price>" +
// "</book>");
// XmlNode root = doc.FirstChild; //root {Element, Name="book"} System.Xml.XmlNode {System.Xml.XmlElement}
// //XmlNode root = doc.DocumentElement.FirstChild;//root {Element, Name="title"} System.Xml.XmlNode {System.Xml.XmlElement}
// Console.WriteLine("Display the title element...");
// Console.WriteLine(root.FirstChild.OuterXml);
// //Console.WriteLine(root.OuterXml);
// Console.WriteLine(doc.OuterXml);//打印全部
// Console.ReadKey();
// }
//}

//XmlNode.OuterXml 属性 作用:获取表示此节点及其所有子节点的标记。属性值:包含此节点及其所有子节点的标记。
//public virtual string OuterXml { get; }
//public class Sample
//{
// public static void Main()
// {
// XmlDocument doc = new XmlDocument();
// doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
// "<title>Pride And Prejudice</title>" +
// "</book>");
// XmlNode root = doc.DocumentElement;
// // OuterXml includes the markup of current node.
// Console.WriteLine("Display the OuterXml property...");
// Console.WriteLine(root.OuterXml);//XmlNode.OuterXml属性的作用:获取表示此节点及其所有子节点的标记
// // InnerXml does not include the markup of the current node.
// // As a result, the attributes are not displayed.
// Console.WriteLine();
// Console.WriteLine("Display the InnerXml property...");
// Console.WriteLine(root.InnerXml);//XmlNode.InnerXml属性的作用:获取和设置仅代表该节点的子节点的标记
// }
//}

//XmlNode.LastChild 属性 作用:获取节点的最后一个子级。 属性值:节点的最后一个子级。如果没有这样的节点,则返回 空引用(在 Visual Basic 中为 Nothing)。
//public virtual XmlNode LastChild { get; }
//public class Sample {
// public static void Main() {
// XmlDocument doc = new XmlDocument();
// doc.LoadXml("<book ISBN='1-861001-57-5'>" +
// "<title>Pride And Prejudice</title>" +
// "<price>19.95</price>" +
// "</book>");
// XmlNode root = doc.FirstChild;
// Console.WriteLine("Display the price element...");
// Console.WriteLine(root.LastChild.OuterXml);
// }
//}

//XmlText.Value 属性 作用:获取或设置节点的值。XmlDocument和XmlElement类都没有重写此属性
//public override string Value { get; set; }
// 它是重写XmlNode类的XmlNode.Value属性 public virtual string Value { get; set; }

//XmlNode.Name 属性 作用:当在派生类中被重写时,获取节点的限定名。属性值:节点的限定名。返回的名称取决于节点的 NodeType:
//public abstract string Name { get; } 这个属性是抽象属性,只能被重写
//XmlDocument、XmlElement、XmlText这三个类都重写了此属性
//public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
//public abstract class XmlLinkedNode : XmlNode
//public class XmlElement : XmlLinkedNode
//说明XmlElement类也派生自XmlNode
//public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
//public class XmlDocument : XmlNode
//说明XmlDocument类的基类是XmlNode类
//public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable
//public abstract class XmlLinkedNode : XmlNode
//public abstract class XmlCharacterData : XmlLinkedNode
//public class XmlText : XmlCharacterData
//说明XmlText类也派生自XmlNode
//XmlElement类重写(public override XmlNode NextSibling { get; })了XmlNode类的NextSibling属性,但没重写XmlNode.FirstChild属性和XmlNode.LastChild 属性
//XmlDocument类和XmlText类都没重写XmlNode.FirstChild属性、XmlNode.LastChild属性、XmlNode.NextSibling属性
//但这三个属性都是虚拟的,所以可以继承,所以这三个虚拟属性都可以继承使用
//XmlDocument、XmlElement、XmlText这三个类都继承了XmlNode.OuterXml属性,所以可以直接用
XMLFile1.xml
<?xml version="1.0" encoding="utf-8"?>
<stories>
<story>
<title>A House in Aungier Street</title>
<author>
<name>Sheridan Le Fanu</name>
<nationality>Irish</nationality>
</author>
<rating>eerie</rating>
</story>
<story>
<title>The Signalman</title>
<author>
<name>Charles Dickens</name>
<nationality>English</nationality>
</author>
<rating>atmospheric</rating>
</story>
<story>
<title>The Turn ofthe Screw</title>
<author>
<name>Henry James</name>
<nationality>American</nationality>
</author>
<rating>a bit dull</rating>
</story>
<story>
<title>lannanzaixian</title>
<author>
<name>litao</name>
<nationality>China</nationality>
</author>
<rating>exciting</rating>
</story>
</stories>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  .NET 职场 休闲 XML知识
相关文章推荐