My winform application communicates with the web service. For some requirement, I need to present the web service response in the XML format in the winform application.
I can serialize the response type (class) instance to XML string. But when i show that string in the rich text box, it's obviously displayed as continuous string; not as the XML as shown below.
<UserServiceAccesses>
- <UserServiceAccess>
- <Service>
<ID>0</ID>
<LocalID>Loggerr</LocalID>
<SystemID>-1</SystemID>
<ServiceType>U</ServiceType>
<Name>MyLogger</Name>
</Service>
<ClientPermissions />
</UserServiceAccess>
- <UserServiceAccess>
- <Service>
<ID>0</ID>
<LocalID>Logger2</LocalID>
<SystemID>-1</SystemID>
<ServiceType>U</ServiceType>
<Name>MyLogger2</Name>
</Service>
<ClientPermissions />
</UserServiceAccess>
<UserServiceAccesses>
Here, UserServiceAccesses
is the class which has property of type UserServiceAccess
. Then UserServiceAccess
has a properties of type Service
, ClientPermissions
How can I do it? It can be in any form (tree, table, text etc.) but it should be readable as XML. Since there are many web methods we call from application, everytime XML structure would be different and so we cannot have definite schema.
To fill a DataSet with data from XML, use the ReadXml method of the DataSet object. The ReadXml method reads from a file, a stream, or an XmlReader, and takes as arguments the source of the XML plus an optional XmlReadMode argument.
On the File menu, point to New, and click File. Select XML File in the Templates pane and click Open. A new file is opened in the editor.
Add the XML file to Visual Studio ProjectOpen up Visual Studio and create a new project or open an existing one. Add an existing item to the C# project. – Select the XML file we created earlier and press Add. – The file will be added to the project.
Try to apply indent to the xmlText, like:
XDocument xDocument = XDocument.Parse(xmlText);
myRichTextBox.Text = xDocument.ToString();//ToString will format xml string with indent
//as XDocument class overrides ToString and return xml with indent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With