Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Xdocument object from XmlNode

I have a call to a SOAP/XML type web service that is successfully returning an XML response.

So far, I have managed to take the returned object, cast it to an XmlNode object.. and have found the actual data in there as expected. All good.

Now, though, I want to bind my data to a DataGridView in a Windows Form. I saw a nice example here C# DataGridView binding to subset of XML using an XDocument and LINQ to provide a DataSource for the grid that would seem to work really well for me.

The problem I have is that I don't know how to create an XDocument based on the object returned by my call to the web service. How can I do this ?

This is how I have captured the returned data from the webservice.. which works..

' call the webservice  '
Dim rawResults As Object = lw.runQuery(parameter1,parameter2)

Dim testresult As XmlNode = DirectCast(rawResults, XmlNode)

Dim docXml As New XmlDocument
docXml.AppendChild(docXml.ImportNode(testresult, True)).

.etc

Here is the XML returned by the webservice.. (Each "runQueryResult" will become a row in the DataGridView)

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <runQueryResponse xmlns="http://xxx.ddd.com/">
    <runQueryResult>
        <runQueryResponse xmlns="">
        <runQueryRecord>
            <catalogid>513</catalogid>
            <name>Vacuum tube </name>
            <this_month>0</this_month>
            <month_past1>1</month_past1>
            <month_past2>0</month_past2>
            <month_past3>0</month_past3>
            <month_past4>0</month_past4>
            <month_past5>0</month_past5>
            <month_past6>0</month_past6>
        </runQueryRecord>
        <runQueryRecord>
            <catalogid>5311</catalogid>
            <name>Adapter expansion</name>
            <this_month>0</this_month>
            <month_past1>1</month_past1>
            <month_past2>0</month_past2>
            <month_past3>0</month_past3>
            <month_past4>0</month_past4>
            <month_past5>0</month_past5>
            <month_past6>0</month_past6>

                            ... etc.
like image 323
Bazza Formez Avatar asked Feb 16 '23 08:02

Bazza Formez


1 Answers

You can create XDocument directly from XmlNode, so you don't have to create XmlDocument instance:

Dim xDoc As XDocument = XDocument.Load(New XmlNodeReader(testresults))
like image 126
MarcinJuraszek Avatar answered Feb 23 '23 07:02

MarcinJuraszek