使用c#,如何逐行显示XML ?

时间:2022-05-12 01:38:51

Hy,

Hy,

I have for example this xml:

比如这个xml:

<books>
  <book1 name="Cosmic">
    <attribute value="good"/>
  </book1>
</books>  

How can I display it in a listBox control line by line, that the final result it will be a listbox with 5 rows in this case?

如何在列表框控件行中逐行显示它,最终结果将是一个列表框,在本例中为5行?

In this moment I am prasing the XML using LINQ to XML like this:

在这一刻,我正在用LINQ将XML修改成这样:

 foreach (XElement element in document.DescendantNodes())
   {
     MyListBox.Items.Add(element.ToString());
   }

But the final result puts every xml node in one list-box item (including child-nodes).

但是最终的结果将每个xml节点放在一个列表框项中(包括子节点)。

Does anyone has any idea how can I put the xml line by line in list-box items?

有没有人知道如何将xml一行一行地放在列表框的条目中?

Thanks.

谢谢。

Jeff

杰夫

3 个解决方案

#1


4  

A simple solution would use a recursive function like the following:

一个简单的解决方案将使用如下的递归函数:

public void FillListBox(ListBox listBox, XElement xml)
{
    listBox.Items.Add("<" + xml.Name + ">");
    foreach (XNode node in xml.Nodes())
    {
        if (node is XElement)
            // sub-tag
            FillListBox(listBox, (XElement) node);
        else
            // piece of text
            listBox.Items.Add(node.ToString());
    }
    listBox.Items.Add("</" + xml.Name + ">");
}

Of course, this one will print only the tag names (e.g. <book1> in your example) and not the attributes (name="Cosmic" etc.). I’m sure you can put those in yourself.

当然,这个将只打印标记名(例如,示例中的 ),而不打印属性(name=" universe "等)。我相信你可以自己把这些写进去。

#2


1  

If you want to display your raw XML in a list box, use a text stream to read in your data.

如果希望在列表框中显示原始XML,请使用文本流在数据中读取。

using(StreamReader re = File.OpenText("Somefile.XML"))
{
  string input = null;
  while ((input = re.ReadLine()) != null)
  {
    MyListBox.Items.Add(input);
  }
}

#3


0  

Jeff, maybe it would be much easier to implement (and to read/maintain) with a simple TextReader.ReadLine()? I don't know what you are trying to achieve, just a suggestion.

Jeff,也许用一个简单的TextReader.ReadLine()实现(和读取/维护)要容易得多?我不知道你想要什么,只是一个建议。

#1


4  

A simple solution would use a recursive function like the following:

一个简单的解决方案将使用如下的递归函数:

public void FillListBox(ListBox listBox, XElement xml)
{
    listBox.Items.Add("<" + xml.Name + ">");
    foreach (XNode node in xml.Nodes())
    {
        if (node is XElement)
            // sub-tag
            FillListBox(listBox, (XElement) node);
        else
            // piece of text
            listBox.Items.Add(node.ToString());
    }
    listBox.Items.Add("</" + xml.Name + ">");
}

Of course, this one will print only the tag names (e.g. <book1> in your example) and not the attributes (name="Cosmic" etc.). I’m sure you can put those in yourself.

当然,这个将只打印标记名(例如,示例中的 ),而不打印属性(name=" universe "等)。我相信你可以自己把这些写进去。

#2


1  

If you want to display your raw XML in a list box, use a text stream to read in your data.

如果希望在列表框中显示原始XML,请使用文本流在数据中读取。

using(StreamReader re = File.OpenText("Somefile.XML"))
{
  string input = null;
  while ((input = re.ReadLine()) != null)
  {
    MyListBox.Items.Add(input);
  }
}

#3


0  

Jeff, maybe it would be much easier to implement (and to read/maintain) with a simple TextReader.ReadLine()? I don't know what you are trying to achieve, just a suggestion.

Jeff,也许用一个简单的TextReader.ReadLine()实现(和读取/维护)要容易得多?我不知道你想要什么,只是一个建议。