Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to read XML with attributes

Tags:

c#

.net

xml

I have a XML file I would like to read (with xml atrributes). The structure doesn't change, but the content does.

Sample XML: http://www.yr.no/place/Norway/Oslo/Oslo/Oslo/forecast.xml (The URL must be copied and pasted into address field manually or you'll get a 404 it seems.)

I'm unable to make a XSD from it using this code (and using XSD.EXE):

XmlTextReader myXmlTextReader = new XmlTextReader("forecast.xml");
DataSet myDataSet = new DataSet();
myDataSet.ReadXml(myXmlTextReader);
myDataSet.WriteXmlSchema("forecast.xsd");

Results in:

Column name 'name' is defined for different mapping types.

Now, imagine that I'm lazy on top of that and don't feel like spending my whole evening manually mapping XML to my objects. I want managed code objects to be generated for me so I can easily read the data.

What are my options?

...and if anyone knows, what does the XSD convert error message mean?

like image 738
Tedd Hansen Avatar asked Jan 24 '26 05:01

Tedd Hansen


2 Answers

If you are going after XML attributes, then why not just load the xml into a XmlDocument. Then you can access the various nodes and attributes.

like image 199
Tony Abrams Avatar answered Jan 25 '26 19:01

Tony Abrams


To answer your final point about the Xsd.exe error, it's having problems with the multiple location elements. If you save the xml and rename the first location element to something like baselocation then it will convert okay.

You may then be able to manually amend the xsd or generated classes to handle this.

like image 34
Craig T Avatar answered Jan 25 '26 19:01

Craig T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!