XML的一部分:
<section name="header">
<placeholder name="HeaderLeftPane"></placeholder>
<placeholder name="HeaderCenterPane"></placeholder>
<placeholder name="HeaderRightPane"></placeholder>
</section>
在页面加载中我有
XmlDocument XD = new XmlDocument();
XD.Load(System.Web.HttpContext.Current.Server.MapPath("Test.xml"));
XmlNodeList nodeListName = XD.SelectNodes("layout/child::node()");
foreach (XmlNode xNode in nodeListName)
{
string name = xNode.Attributes["name"].Value;
int child = xNode.ChildNodes.Count;
LblDisplay.Text += name + child;
LblDisplay.Text += "<br/>";
}
结果是header3
。我希望捕获所有子placeholder
属性name
。我怎样才能抓住它来帮助它。
答案 0 :(得分:1)
XmlDocument XD = new XmlDocument();
XD.Load(System.Web.HttpContext.Current.Server.MapPath("Test.xml"));
XmlNodeList listChildNodes = XD.SelectNodes("//*[name()='placeholder']");
List<string> names = new List<string>(listChildNodes.Count);
foreach (XmlNode nodeChild in listChildNodes)
{
names.Add(nodeChild.Attributes["name"].InnerText);
}
Console.WriteLine(string.Join(";", names.ToArray()));
答案 1 :(得分:0)
我认为你正在围绕所有的地方主持人,但是压倒你的变化。你需要做的是在foreach循环中将名称添加到列表中,然后在文本框中显示
示例
XmlDocument XD = new XmlDocument();
XD.Load(System.Web.HttpContext.Current.Server.MapPath("Test.xml"));
XmlNodeList nodeListName = XD.SelectNodes("section/placeholder");
IList<string> names = new List<string>();
foreach (XmlNode xNode in nodeListName)
{
string name = xNode.Attributes["name"].Value;
int child = xNode.ChildNodes.Count;
names.Add(name);
}
显示像这样的属性
string nameString = string.Empty;
foreach (string name in names)
{
nameString += name + " ";
}
LblDisplay.Text = nameString;