Reading XML into C#
Posted on 2004-09-12
This question can be found in many places across the net and the answer usually involves the ReadElementString method.
But... Why would I ask a 500 point question for something that easy you ask??? I've spoken to a lot of people about the data I'm using and they basically don't know because they know that it's awful XML and it's not elemented really.
Well... It's like this.
I need to extract values like the following and put them into an editor.
<string id="name" value="my file"/>
<string id="auth" value="Dan Atkinson"/>
<string id="desc" value="This is Dan's file"/>
<string id="vers" value="1.0"/>
<colour id="bgcl" red="255" green="0" blue=""/>
<real id="bgop" value="0.2"/>
<integer id="cmod" value="1"/>
<colour id="ppcl" red="255" green="255" blue="255"/>
I am using Visual C# 2005 Express and it's helping me a lot but I've come unstuck. I've scoured for an answer for this.
Here is what I have...
xtr = new XmlTextReader(openFileDialog1.FileName);
String strSkinName = xtr.ReadString();
String strAuthor = xtr.ReadElementString();
String strDescription = xtr.ReadElementString();
String strVersionNo = xtr.ReadElementString();
String strParent = xtr.ReadElementString();
String strBgColRed = xtr.ReadElementString();
String strBgColGreen = xtr.ReadElementString();
String strBgColBlue = xtr.ReadElementString();
String strBgOpacity = xtr.ReadElementString();
String strColourMode = xtr.ReadElementString();
String strMenuPopTxtColourRed = xtr.ReadElementString();
String strMenuPopTxtColourGreen = xtr.ReadElementString();
String strMenuPopTxtColourBlue = xtr.ReadElementString();
this.skinName.Text = strSkinName;
this.author.Text = strAuthor;
this.description.Text = strDescription;
this.versionNo.Text = strVersionNo;
this.parent.Text = strParent;
this.bgColRed.Text = strBgColRed;
this.bgColGreen.Text = strBgColGreen;
this.bgColBlue.Text = strBgColBlue;
this.bgOpacity.Text = strBgOpacity;
this.colourMode.Text = strColourMode;
this.menuPopTxtColourRed.Text = strMenuPopTxtColourRed;
this.menuPopTxtColourGreen.Text = strMenuPopTxtColourGreen;
this.menuPopTxtColourBlue.Text = strMenuPopTxtColourBlue;
I already have the corresponding boxes set up in a form and it works ONLY if I convert the data into elements. Otherwise, the xmlTextReader doesn't understand it.
I am aware that some of these are not strings and some are integers and some are real numbers. But I wanted to get at least a few of the sections reading in the xml before I start tackling the data types.
The data cannot be converted into standard elements (I know it can but the program that utilises it cannot understand it).
Any help on this would be greatly appreciated.
Also, for an added bonus...
Is there any way I can prevent the xmlTextReader accessing the .dtd file online (which points to a 404 address) when it loads? By preventing I mean bypassing the file.
And, as you can see, the two colour id's in the data section are split into RGB values and yet I want them to be shown seperately (in 3 RGB combo boxes (Red, Green, Blue all 0-255)). Is this possible to break it down into this?
Once all that is done I'll then have to recompile it all back into its rubbish form in an xml file. That may or may not be easy but if I can't do it then it will be a seperate question.
Thanks in advance.