简体   繁体   中英

How to display xmlnodes in textbox

I'm curious how you can display a specific node content in a textbox

My XML file:

<?xml version="1.0"?>
<root>
  <debug_mode>true</debug_mode>
  <filter>
    <filter_item>1158</filter_item>
    <filter_item>1159</filter_item>
    <filter_item>1160</filter_item>
  </filter>
</root>

My cs file:

 public MainWindow()
        {
            InitializeComponent();

            XmlDocument Xdoc = new XmlDocument();

            Xdoc.Load(xmldoc);
            XmlElement el = (XmlElement)Xdoc.SelectSingleNode("root/filter/filter_item");         
            tbOrderDisplay.Text = el.InnerText;

        }

Innertext sadly doesn't display anything in my textbox, is there a way to apply a foreach to show every item? (I'm still learning how to work with c#)

Something like this should work:

var root = XDocument.Load(xmldoc).Root;
var filter = root.Element("filter");

foreach (var filterItem in filter.Descendants("filter_item"))
{
    tbOrderDisplay.Text += filterItem.Value + "\n";
}

Or shorter:

tbOrderDisplay.Text = string.Join("\n",
    filter.Descendants("filter_item").Select(f => f.Value));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM