0

I'm working with a 3rd party application that takes input in via XML, and then returns the input back out in XML, I'm looking for a way to format the information to display it nicely in a richtextbox.

<Solution>
  <ID>1</ID>
  <Property>
    <Name>DriverSheave</Name>
    <Value>1VP34</Value>
  </Property>
  <Property>
    <Name>DriverBushing</Name>
    <Value>
    </Value>
  </Property>
  <Property>
    <Name>DrivenSheave</Name>
    <Value>AK49</Value>

this is some sample xml that i would receive as an output from the 3rd party app, What I'm currently doing is this.

richTextBox1.Text = Configurator.Results.InnerText.ToString();

which gives me results like this.

1DriverSheave3MVP55B69DriverBushingDrivenSheave3MVB200RDrivenBushingR1BeltB225BeltQty3

essentially id like to know the best way to move these around, and make the output formatted nicely. so im not asking that you format this for me, but rather let me know the proper way to go about formatting this.

id like it to look similar toThis

  • 1
    Look at this question and accepted answer: http://stackoverflow.com/questions/1123718/format-xml-string-to-print-friendly-xml-string – Icemanind Mar 20 '15 at 15:27
  • this is a sample screen shot of how other people have the output from this 3rd party app displayed however the 3rd party has no faq, or help just sample screen shots of potential. –  Mar 20 '15 at 15:38
  • You need to actually parse the XML into its constituent components, and then display those the way you like. – Cameron Mar 20 '15 at 15:39
  • Why don't use TreeView? its very nicely show result for you. i have its code if you want. – Rashed DIP Mar 20 '15 at 15:42
  • @Cameron that sounds like what i want to do, im just unsure of how to go about it, Xml has been thrust upon me so i have very little knowledge in handling it. –  Mar 20 '15 at 15:44
  • @RashedDIP that sounds interesting, i wouldn't mind taking a look at it. –  Mar 20 '15 at 15:45
  • What types are `Configurator.Results` – Chuck Savage Mar 20 '15 at 16:12
  • @Cameron is right, and you would need to parse through the XML and create the tree accordingly. Check out the `XmlDocument` class, it's probably the easiest to work with. [Here's](http://www.codeproject.com/Articles/169598/Parse-XML-Documents-by-XMLDocument-and-XDocument) a tutorial on the class as well – amura.cxg Mar 20 '15 at 16:16
  • @ChuckSavage Configurator.Results, is the xml results I'm receiving back from the 3rd party app, the XLM sample in my question is a segment of that XML –  Mar 20 '15 at 16:21
  • What class Types, I get what they hold, but if we're to help you, we need to know the types. Anyway, see my answer below in a second to see if it helps you. – Chuck Savage Mar 20 '15 at 16:25

2 Answers2

0

Refer to the below code:

using System.XML.Linq;

class XMLParseProgram
{
public DataTable ReadXML(string strXML)
{
  XDocument xdoc = XDocument.Load(strXML);

  var property= from props in xdoc.Element("Solution").Elements("Property").ToList().ToList();

if (property!= null)
            {

                DataTable dtItem = new DataTable();
                dtItem.Columns.Add("Name");
                dtItem.Columns.Add("Value");

foreach (var itemDetail in property.ElementAt(0))
                {
                    dtItem.Rows.Add();

                    if (itemDetail.Descendants("Name").Any())
                        dtItem.Rows[count]["Name"] = itemDetail.Element("Name").Value.ToString();

                    if (itemDetail.Descendants("Value").Any())
                        dtItem.Rows[count]["Value"] = itemDetail.Element("Value").Value.ToString();
}
}
}

}

You can use the DataTable to generate the string in your program the way you want.

Sormita Chakraborty
  • 1,015
  • 2
  • 19
  • 36
0

If one of Configurator or Configurator.Results is an XmlDocument or XmlElement you can use one of these converters to an XElement:

richTextBox1.Text = Configurator.Results.ToXElement().ToString();

enter image description here

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
  • this question may sound slightly stupid, but essentially i could fill that XElement with this `("Record","Address Record", new XElement("Name", new XElement("First","Mike"), new XElement("Second")), new XElement("Address"));` and it would display everything out like first line: record, address second line: name, third line: first mike, or is this not how it would work –  Mar 20 '15 at 16:32
  • @AndrewKucenski Why fill it if the converter will take your results and convert it to an `XElement` for you? But yes you could. An XElement `.ToString()` method will do formatted output by default. – Chuck Savage Mar 20 '15 at 16:35