LINQ读取XML文件并打印结果

时间:2011-12-28 19:39:34

标签: c# .net linq linq-to-xml

我得到了以下XML文件(Data.xml):

<root>
 <sitecollection name="1A">
   <site name="1B">
     <maingroup name="1C"> 
        <group name="1D"> </group>
     </maingroup> 
    </site>
 </sitecollection>
 <sitecollection name="2A">
   <site name="2B">
     <maingroup name="2C"> 
        <group name="2D"> </group>
     </maingroup> 
    </site>
 </sitecollection>
</root>

我需要以这种格式打印所有子元素:

1A
 1B
  1C
   1D
2A
 2B
  2C
   2D

到目前为止,我有以下代码,需要进行一些调整。如果有一个更简单的方法,我也可以完全改变它。谢谢你的帮助

class xmlreader
{
    public static void Main()
    {

        // Xdocument to read XML file
        XDocument xdoc = XDocument.Load("Data.xml");
        var result = new System.Text.StringBuilder();

        var lv1s = from lv1 in xdoc.Descendants("sitecollection")
                   select new
                   {
                       sitecollection = lv1.Attribute("name").Value,
                       maingroup = lv1.Descendants("group")

                   };
        var lv2s = from lv2 in xdoc.Descendants("site")
                   select new
                   {
                       site = lv2.Attribute("name").Value,
                       sitetittle = lv2.Descendants()
                   };
        var lv3s = from lv3 in xdoc.Descendants("maingroup")
                   select new
                   {
                       maingroup = lv3.Attribute("name").Value,
                   };
        var lv4s = from lv4 in xdoc.Descendants("group")
                   select new
                   {
                       grouppage = lv4.Attribute("name").Value,
                   };


        // Loop to print results
        foreach (var lv1 in lv1s)
        {
            result.AppendLine(lv1.sitecollection);
            foreach (var lv2 in lv2s)
            {
                result.AppendLine("   " + lv2.Attribute("name").Value);

                foreach (var lv3 in lv3s)
                {
                    result.AppendLine("   " + lv3.Attribute("name").Value);

                    foreach (var lv4 in lv4s)
                    {
                        result.AppendLine("   " + lv4.Attribute("name").Value);
                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

使用这种统一的层次结构,递归可以用更少的代码完成工作:

    void PrintNames(StringBuilder result, string indent, XElement el)
    {
        var attr = el.Attributes("name");
        if (attr != null)
        {
            result.Append(indent);
            result.Append(attr.Value);
            result.Append(System.Environment.NewLine);
        }
        indent = indent + " ";
        foreach(var child in el.Elements())
        {
            PrintNames(result, indent, child);
        }
    }

...

var sb = new StringBuilder();
PrintNames(sb, String.Empty, xdoc.Root);

答案 1 :(得分:0)

以下内容如何,​​查找具有name属性的所有元素,然后根据其深度添加空格。

var result = new System.Text.StringBuilder();
var namedElements = doc.Descendants().Where(el => el.Attributes("name")!=null);
foreach(var el in namedElements)
{
  int depth = el.Ancestors().Count();
  for (int i=0;i<depth;i++)
    result.Append(" "); 

  result.Append(el.Attributes("name").Value);
  result.Append(System.Environment.NewLine);
}

注意:上面是内存,所以请检查语法!