把做工程过程中较好的一些代码片段珍藏起来,下面资料是关于C# 读写xml文件的的代码,应该对各朋友有较大用途。
using System;
using System.Xml;
class TestReadXML
{
public static void Main()
{
XmlTextReader reader = new XmlTextReader(C:\test.xml);
reader.Read();
while (reader.Read())
{
reader.MoveToElement();
Console.WriteLine(XmlTextReader Properties Test);
Console.WriteLine(===================);
Console.WriteLine(Name: + reader.Name);
Console.WriteLine(Base URI: + reader.BaseURI);
Console.WriteLine(Local Name: + reader.LocalName);
Console.WriteLine(Attribute Count: + reader.AttributeCount.ToString());
Console.WriteLine(Depth: + reader.Depth.ToString());
Console.WriteLine(Line Number: + reader.LineNumber.ToString());
Console.WriteLine(Node Type: + reader.NodeType.ToString());
Console.WriteLine(Attribute Count: + reader.Value.ToString());
}
}
}
写XML文件:
using System;
using System.Xml;
public class TestWriteXMLFile
{
public static int Main(string[] args)
{
try
{
XmlTextWriter writer = new XmlTextWriter(C:\temp\xmltest.xml, null);
writer.WriteStartDocument();
writer.WriteComment(Commentss: XmlWriter Test Program);
writer.WriteProcessingInstruction(Instruction,Person Record);
writer.WriteStartElement(p, person, urn:person);
writer.WriteStartElement(LastName,);
writer.WriteString(Chand);
writer.WriteEndElement();
writer.WriteStartElement(FirstName,);
writer.WriteString(Mahesh);
writer.WriteEndElement();
writer.WriteElementInt16(age,, 25);
writer.WriteEndDocument();
}
catch (Exception e)
{
Console.WriteLine (Exception: {0}, e.ToString());
}
return 0;
}
}