【发布时间】: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..