如何读取 xmlnode 和内部子节点并从输入类型文本进行更新
本文关键字:输入 类型 文本 更新 子节点 内部 何读取 读取 xmlnode | 更新日期: 2023-09-27 18:35:28
我有一个这样的xml文件...
<AnimationPackage>
<condition>
<name>Some Condition 0</name>
<url>http://www.google.com.pk</url>
<rehab>
<name>Some rehab 0</name>
<url>http://www.youtube.com</url>
</rehab>
<rehab>
<name>Some rehab 1</name>
<url>http://www.youtube.com</url>
</rehab>
<rehab>
<name>Some rehab 2</name>
<url>http://www.youtube.com</url>
</rehab>
</condition>
<condition>
<name>Some Condition 1</name>
<url>http://www.google.com.pk</url>
<rehab>
<name>Some rehab 0</name>
<url>http://www.youtube.com</url>
</rehab>
<rehab>
<name>Some rehab 1</name>
<url>http://www.youtube.com</url>
</rehab>
如何计算标签名称也像 首先 <Conditions>
然后<rehab>
对于那个条件然后是 <rehab>
标记中的内部文本 <name>
和 <url>
标记。我试过这个,但第一次阅读后它不起作用。.请提供任何帮助。
XmlDocument readDoc = new XmlDocument();
readDoc.Load(MapPath("JkGroups.xml"));
conditions = readDoc.SelectNodes("AnimationPackage/condition").Count;
rehab = readDoc.SelectNodes("AnimationPackage/condition/rehab").Count;
lblApptitle.Text = dt.Rows[0]["packageName"].ToString();
XmlNodeList nodeListName = readDoc.SelectNodes("AnimationPackage/condition/rehab/name");
XmlNodeList nodeListUrl = readDoc.SelectNodes("AnimationPackage/condition/rehab/url");
for (rehabCount = 0; rehabCount < rehab; rehabCount++)
{
rehabNodeName = nodeListName[0].ChildNodes[rehabCount].InnerText;
rehabNodeUrl = nodeListUrl[0].ChildNodes[rehabCount].InnerText;
rehabtr += "<tr><td style='width: 106px;padding-left: 20px;' >Rehab title</td><td>"
+ "<input type='text' value='" + rehabNodeName + "' id='txtRehabSupportTitle" + rehabCount + "' runat='server' /></td><td>"
+ "<input type='text' value='" + rehabNodeUrl + "' id='txtRehabSupportURL" + rehabCount + "' runat='server' /></td></tr>";
}
string rehabBody = "<table id='tblRehab'>" + rehabtr + "<tr><td></td><td></td><td><p>Type support product buy URL</p></td></tr><tr>"
+ "<td><td> </td><td> </td></tr></table>";
缺少一些代码,但我只想在 xml 中提供帮助。
试试这段代码
XDocument doc = XDocument.Load("c://test.xml");
var animationPackage = doc.Descendants("AnimationPackage");
int count = 0;
foreach (XElement condition in animationPackage.Descendants("condition"))
{
count++;
String name = condition.Element("name").Value;
}
//print count . .
或者
,您可以使用更多的 linq 功能并在一个表达式中获取计数:
XDocument doc = XDocument.Load(@"c:'test.xml");
int rehabCount = doc.Root.Descendants()
.Where(x => x.Name == "rehab").Count(); // 5
var rehabValues = doc.Root.Descendants("rehab")
.Elements().Select(tag => tag.Value);
结果:
康复计数:5
康复价值观:
IEnumerable<String>
(10 项):
Some rehab 0
http://www.youtube.com
Some rehab 1
http://www.youtube.com
Some rehab 2
http://www.youtube.com
Some rehab 0
http://www.youtube.com
Some rehab 1
http://www.youtube.com
XDocument xDoc = XDocument.Load(......);
var result = xDoc.Descendants("condition")
.Select(c => new {
Name = c.Element("name").Value,
Rehabs = c.Descendants("rehab")
.Select(r=>new {
Name = r.Element("name").Value,
Url = r.Element("url").Value
})
.ToArray()
})
.ToArray();
Console.WriteLine("Condition#: " + result.Length);
foreach (var cond in result)
{
Console.WriteLine("'tCondition[" + cond.Name + "]: " + cond.Rehabs.Length);
foreach (var rehab in cond.Rehabs)
{
Console.WriteLine("'t't"+ rehab.Name + " , " + rehab.Url);
}
}