user4622594
user4622594

Reputation:

Display XML Content dynamically

I'm currently working on a solution that works with a WebService which is hosted by a government (So no chance to change anything on this side ;-) ).

The method I am using returns a XML string - so far so good.

The problem now is: The XML returned can have a lot of different structures and there is no way to find out which one is returned. No XSD or anything else available....

Example Pseudo Code:

string XmlFileContent = WebService.MethodGetXMLFile(filekey)

The XmlFileContent can e.g. look like this:

<XML>
<Field1>sometext</Field1>
</XML>

or

<Table Date ="20150302" Time = "0946">
<Row>
<Field1>sometext</Field1>
<Field2>2341.5145</Field2>
</Row>
</Table>

or any kind of XML you can think of...

So the question is: are there any possibilities or tools to display this XML Content in an acceptable way? Formatting can be ignored, i just want to display the Data in a better way than showing the plain XML Text.

The Client which works with the WebService is written in C# (.NET 4.5), the technology for displaying the XML Data doesn't matter - anything that helps is perfekt. (Maybe HTML or kind of that!?)

Upvotes: 2

Views: 861

Answers (1)

Grzegorz Fedczyszyn
Grzegorz Fedczyszyn

Reputation: 344

As long as the 1st example is a mistake (it is not a correct xml) you can try something like this if you only want to display it. You will have to add some formatting obviously but it is itill more user friendly than plain xml.

     [Test]
    public void test()
    {
        var a = @"<XML>
        <Field1>sometext</Field1>
        </XML>";

        var b = @"<Table Date ='20150302' Time = '0946'>
       <Row>
       <Field1>sometext</Field1>
       <Field2>2341.5145</Field2>
       </Row>
       </Table>";

        XDocument doc=XDocument.Parse(b);
        PrintAllNodes(doc.Descendants());
    }

    private void PrintAllNodes(IEnumerable<XElement> nodes)
    {
        foreach (var node in nodes)
        {
            foreach (var xAttribute in node.Attributes())
            {
                Console.WriteLine(xAttribute.Name + ": " + xAttribute.Value);
            }

              Console.WriteLine(node.Name + " " + node.Value);
        }

    }

Upvotes: 1

Related Questions