0

我有一个这样的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>

如何计算标签名称也像 Atfirst<Conditions>然后<rehab>For that condtions 然后来自<rehab>标签的内部文本<name><url>标签。我试过这个,但在第一次阅读它不起作用..任何帮助PLZ。

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>&nbsp;</td><td>&nbsp;</td></tr></table>";

缺少一些代码,但我只想在 xml 中提供帮助。

4

3 回答 3

0

试试这个代码

        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 . .
于 2012-05-25T06:29:23.887 回答
0

或者您可以使用更多 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
于 2012-05-25T07:00:15.653 回答
0
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);
    }
}
于 2012-05-25T07:04:14.153 回答