日期:2014-05-20  浏览次数:20820 次

请问如何读取xml文件并按照xml文件的格式完整输出/?/?
以下面的xml文件为例

<?xml   version= "1.0 "   encoding= "utf-8 "?>
<Trans>
    <Items   Key= "btnInput "   id= "输入 "   />
    <Items   Key= "lblDisplay "   id= "显示 "   />
    <Items   Key= "btnChangeLanguage "id= "多语言 "   />
</Trans>

输出结果也为

<?xml   version= "1.0 "   encoding= "utf-8 "?>
<Trans>
    <Items   Key= "btnInput "   id= "输入 "   />
    <Items   Key= "lblDisplay "   id= "显示 "   />
    <Items   Key= "btnChangeLanguage "id= "多语言 "   />
</Trans>

希望大侠们能指点一下或给个相关例子`~谢谢



------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream( "C:\\new.xml ", FileMode.Open);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
Console.Write(sr.ReadToEnd());
sr.Close();
fs.Close();
Console.Read();
}
}
}

------解决方案--------------------
TO:怎样得到Key这个属性的属性名/?

XmlDocument doc = new XmlDocument();
doc.Load(@ "../../test.xml ");
XmlNodeList nodes = doc.SelectNodes(@ "//Items ");
foreach (XmlNode node in nodes)
{
for (int i = 0; i < node.Attributes.Count; i++)
{
Console.WriteLine( "属性名: " + node.Attributes[i].Name + "属性值: " + node.Attributes[i].Value);
}
}