【问题标题】:Reading Custom XML File读取自定义 XML 文件
【发布时间】:2013-11-26 15:38:11
【问题描述】:

这是 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();
}

【问题讨论】:

  • 列表项的值和键是什么?
  • 姓名:年龄:小时:原因:资格:(每一项都是列表框中的新项目)
  • 如果你使用LINQ to XML会容易得多!!

标签: c# xml


【解决方案1】:

使用 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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      相关资源
      最近更新 更多