C# .NET - Reading Xml into a textbox

Asked By Daan Mouha on 27-Apr-11 04:59 AM
Hey

I want to read an xml-file and put it directly into a text box (multi lined).
Here is the Xml-file, source code and the output.

Xml-file

<?xml version="1.0"?>
<Networks>
  <NW Alias="Alias">
  <NWDevice Type="Coordinator" Mac="0013A20046666666" Version="Z3.2.6666 1035 N" Hardware="RM015_LOG">
    <Sensor Type="Humidity" Key="1101">
    </Sensor>
 
    <Sensor Type="Humidity" Key="1102">
    <Line MeasureTime="100" fromValue="60" toValue="70" Period="200"/>
  <Line MeasureTime="15" fromValue="30" toValue="50" Period="30"/>
    </Sensor>
 
    <Sensor Type="Temperature" Key="1001">
  <Line MeasureTime="1" fromValue="20" toValue="30" Period="3000"/>
    </Sensor>
 
    <Sensor Type="CO2" Key="1601">
    <Line MeasureTime="15" fromValue="300" toValue="400" Period="45"/>
    </Sensor>
  </NWDevice>
</Networks>

The code:

StreamReader reader = new StreamReader(sFileName);
while (Convert.ToBoolean(reader.Read()))
{
  sLine = reader.ReadLine();
  if (sLine == null)
  {
    break;
  }
  txtXmlEditor.Text += sLine;
  txtXmlEditor.Text += Environment.NewLine;
}

The Output:
Here you find the problem, At places there is no beginning '<' and there are places with to many spaces between the text. I want to work with so few as possible if structures.

?xml version="1.0"?>
Networks>
NW Alias="Alias">
    
  <NWDevice Type="Coordinator" Mac="0013A20046666666"   Version="Z3.2.6666 1035 N" Hardware="RM015_LOG">
 
    <Sensor Type="Humidity" Key="1101">
    </Sensor>
     
    <Sensor Type="Humidity" Key="1102">
    <Line MeasureTime="100" fromValue="60"            toValue="70" Period="200"/>
    <Line MeasureTime="15" fromValue="30"           toValue="50" Period="30"/>
    </Sensor>
 
  <Sensor Type="Temperature" Key="1001">
    <Line MeasureTime="1" fromValue="20"
    toValue="30" Period="3000"/>
  </Sensor>
 
    <Sensor Type="CO2" Key="1601">
      <Line MeasureTime="15" fromValue="300"          toValue="400" Period="45"/>
    </Sensor>
   </NWDevice>
/NW>
/Networks>


PS. Sorry for my bad English.
Sahil Kumar replied to Daan Mouha on 27-Apr-11 05:08 AM
Hi,

You can read complete text in text format or xml format, choice is yours

XmlDocument xmldoc = new XmlDocument();

xmldoc.Load(path);

string text= xmldoc.InnerText;

string xmltext = xmldoc.InnerXml;


this code will gives you both text and xmltext. just pass path of file into as input to this code.

I hope this will solve your issues.
Riley K replied to Daan Mouha on 27-Apr-11 05:13 AM
See this below example

 
<?xml version="1.0"?>
           <Verzeichnis>
             <Anz_Messstellen>3</Anz_Messstellen>
             <Messstelle Nummer="1">
               <Bezeichnung>Temp1</Bezeichnung>
             </Messstelle>
             <Messstelle Nummer="2">
               <Bezeichnung>Temp2</Bezeichnung>
             </Messstelle>
             <Messstelle Nummer="3">
               <Bezeichnung>Temp3</Bezeichnung>
             </Messstelle>
           </Verzeichnis>


dynamic messtellen = from el in xdoc.Descendants("Verzeichnis").Descendants("Messstelle");
 
// if you want to know how manny descendants there are you could just read out the count
//  MessageBox.Show(messtellen.Count)
 
System.Text.StringBuilder textvalues = new System.Text.StringBuilder(100);
 
foreach (object messtelle_loopVariable in messtellen) {
  messtelle = messtelle_loopVariable;
 
  dynamic v = from tval in messtelle.Descendants("Bezeichnung");
 
  textvalues.AppendLine(v.Value);
}
 
MessageBox.Show(textvalues.ToString());
Daan Mouha replied to Sahil Kumar on 27-Apr-11 05:29 AM
It works but it gives another problem.
How do you get it vertically?

Output:

<?xml version="1.0"?><Networks><NW Alias="Alias">

Daan Mouha replied to Sahil Kumar on 27-Apr-11 05:33 AM
Thanks for replying
Daan Mouha replied to Daan Mouha on 27-Apr-11 05:42 AM
I fixed this problem by an
string.replace(">", ">" + Environment.NewLine);

How can I put jump-in to it so it reads easier?