开发者

C# XML to Listbox Help

开发者 https://www.devze.com 2023-04-04 23:32 出处:网络
I have XML that is being returned back from a rest service WCF. The following is an example of the XML

I have XML that is being returned back from a rest service WCF. The following is an example of the XML

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<catalog version="1.1"><dataset id="354" name="Mariano - Ship Drift" description="Global Currents, Static" datatype="currents" rank="5" sarops开发者_Python百科rank="4" format="netcdf" starttime="1980-01-01T00:00:00" endtime="2019-01-01T00:00:00" extentleft="-180.0000" extentbottom="-90.0000" extentright="180.0000" extenttop="90.0000" source="STATIC" wmslayeridstr="MARIANO_currents" confidence="L" directionfrom="no" image="ne_ndbc.jpg" />
</catalog>
</string>

I need to get the value from id, name, description, etc... and put it into a list or a listbox.

WebResponse response = restWebRequest.GetResponse();
Stream responseStream = response.GetResponseStream();

Reponse stream is the stream that holds the XML.

Any ideas?


XDocument doc = XDocument.Load(responseStream);

var elem = doc.Descendants().FirstOrDefault(el => el.Name.LocalName == "dataset");
if(elem != null)
{
    var attID = elem.Attribute("id");
    if(attID != null)
        Console.WriteLine(attID.Value);
}

OR

You could directly get the IEnumerable with an anonymous type:

XDocument doc = XDocument.Parse(xml);

var values = doc.Descendants("dataset")
                .Select(el => new { id = el.Attribute("id").Value, 
                                    name = el.Attribute("name").Value 
                                  }
                 );


Here's another approach: (I tested your XML by loading it from a file.)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Xml.Serialization;
using System.IO;

namespace delete4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Load();
        }

        void Load()
        {
            var stream = new FileStream("c:\\pj\\data.txt", FileMode.Open);

            XmlRootAttribute xRoot = new XmlRootAttribute();
            xRoot.ElementName = "string";
            xRoot.IsNullable = true;
            xRoot.Namespace = "http://schemas.microsoft.com/2003/10/Serialization/";

            XmlSerializer s = new XmlSerializer(typeof(sstring), xRoot);

            var o = s.Deserialize(stream) as sstring;  // o is your loaded object 

            stream.Close();
        }

        [ XmlRoot("string"), XmlType("string")]
        public class sstring
        {
            public Catalog catalog;
        }

        public class Catalog
        {     
            public Dataset dataset;
        }

        public class Dataset
        {
            [XmlAttribute("name")]
            public string Name;
            [XmlAttribute("id")]
            public string ID;
            [XmlAttribute("description")]
            public string Description;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消