【问题标题】:Read values from xml and add them into list从 xml 读取值并将它们添加到列表中
【发布时间】:2014-08-16 05:32:52
【问题描述】:

如何从此 xml 文件中读取值(image1、image2、image3)。 它是一个样本,我不知道确切的值的数量。我的意思是 xml 文件可能有更多的 Image Name 。我想将这些图像名称添加到列表中。我还想获取 ImageInterval 值并将其设置为 int 变量。

    <Sinage DataBase>

    <Image>
        <ImageName>
            image1
        </ImageName>

        <ImageName>
            image2
        </ImageName>

        <ImageName>
            image3
        </ImageName>

        <ImageInterval>
            4
        </ImageInterval>
    </Image>
</Sinage DataBase>

这是我的清单:

public List<string> ImageName { get; set; }

我使用上面的代码读取数据但显示错误:Illegal character in path

XDocument doc = XDocument.Load(global::TestGraphic.Properties.Resources.xml);

    var ImageName = doc.Descendants("ImageName");

    foreach (var image in ImageName)
    {
        MessageBox.Show(image.Value);
    }

【问题讨论】:

  • 这部分我不熟悉:global::TestGraphic.Properties.Resources.xml
  • @har07 xml 是我的 xml 文件的名称
  • 从标签中的“Sinage DataBase”中删除空格,在XML中无效,您可以在标签中使用“SinageDataBase”。它会正常工作的。
  • global::TestGraphic.Properties.Resources.xml 是文件本身,还是文件的路径?如果是文件,则应使用XDocument.Parse() 而不是XDocument.Load()
  • 你是对的@MarcinJuraszek 它有效。谢谢。请将您的评论作为答案 plz..

标签: c# xml


【解决方案1】:

如果global::TestGraphic.Properties.Resources.xml 是文档内容本身,而不是文件路径,则应使用XDocument.Parse() 而不是XDocument.Load()

【讨论】:

    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 2014-06-03
    • 2017-05-24
    • 2021-08-23
    • 2021-04-04
    • 1970-01-01
    相关资源
    最近更新 更多