读取自定义XML文件

时间:2013-11-26 15:38:12

标签: c# xml

这是Guardian.re,它是我在C#中创建的自定义XML文件。我创建了一个新程序,我想读取此文件并将信息放在列表框中。 xmlns是输入的用户的名称。我该怎么做?

<?xml version="1.0" encoding="utf-8"?>
<!--This is the Guardian Submit file. An admin will read with their viewers.-->
<Guardian 
    Age="5" 
    Hours="5" 
    Why="Why?" 
    Qualify="What qualifies you?" 
    xmlns="Name" />

这是C#来源:

 if (System.IO.File.Exists("Guardian.re") == false)
            {
                //.re is the file extension that is used

                XmlWriter writer = XmlWriter.Create(@"Guardian.re");

                writer.WriteStartDocument();

                writer.WriteComment("This is the Guardian Submit file. An admin will read with their viewers.");
                //Element <Guardian> in the .re XML format
                writer.WriteStartElement("Guardian", IGN);



                //the element <Age> in the .re XML format
                writer.WriteAttributeString("Age", Age);

                //the element <Hours> in the .re XML format
                writer.WriteAttributeString("Hours", hours);

                //the element <Why> in the .re XML format
                writer.WriteAttributeString("Why", WhyRank);

                //the element <Qualify> in the .re XML format
                writer.WriteAttributeString("Qualify", Qualify);
                writer.WriteEndElement();



                writer.Flush();
                writer.Close();
}

2 个答案:

答案 0 :(得分:0)

使用XPath从文件中找回您的个人值

http://support.microsoft.com/kb/308333

然后将值添加到此处指定的列表框

http://msdn.microsoft.com/en-us/library/aa288403(v=vs.71).aspx

您还可以在Parse XML and populate in List Box

找到一些代码

答案 1 :(得分:0)

我在这里发布了一个可能的答案:Reading an XML File With Linq
猜猜我可能应该等待,因为另一个是关于Linq ......