首页 > Windows开发 > 详细

C# 读写xml文件的源码

时间:2019-06-12 14:45:12      阅读:106      评论:0      收藏:0      [点我收藏+]

把做工程过程中较好的一些代码片段珍藏起来,下面资料是关于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;
}
}




 

C# 读写xml文件的源码

原文:https://www.cnblogs.com/gladiolus/p/11009277.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!