我做了一些从 xml 文件读取的东西,一切都完美。 但有一点是行不通的。
这是工作正常的 xml 部分
<title>lorum ipsum lorum ipsum</title>
这是我想阅读的 xml 部分:
<enclosure url="http://media.nu.nl/m/m1nxf1eaa6mh_sqr256.jpg" type="image/jpeg" />
我只想要变量中的 url。
这就是我到目前为止所拥有的:
switch (node.Name)
{
case "title": label5.Text = (node.InnerText); break;
case "enclosure": string picbox2 = (node.InnerText); break;
pictureBox2.ImageLocation = picbox2;
case "description": label6.Text = (node.InnerText); i++; break;
}
我希望我已经提供了足够的信息。
最佳答案
在“enclosure”情况下,在该情况的 break;
语句之后有一个赋值语句:pictureBox2.ImageLocation = picbox2;
。我不希望它能够编译。
您还需要以 element.Attributes["attr_name"].Value
的形式访问元素属性,而不是使用 InnerText 属性,该属性将带回开始和结束元素标记之间的文本。
switch (node.Name)
{
case "title":
label5.Text = (node.InnerText);
break;
case "enclosure":
string picbox2 = (node.Attributes["url"].Value);
pictureBox2.ImageLocation = picbox2;
break;
case "description":
label6.Text = (node.InnerText);
i++;
break;
}
关于c# - 从 C# 中的 xml 读取闭合标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23778284/