Cannot convert System.Xml.XmlNode to System.Xml.Linq.XElement

11,824

I don't have VB 2008 handy, but to the best of my knowledge there has never been any automatic conversion (implicit or explicit) between XElement and XmlNode - LINQ-to-XML is largely a parallel implementation of a DOM, with only a few things (like XmlReader as a source) in common.

However; in terms of addressing the issue, the best I can suggest is to work with the xml; a C# example (using CreateReader to avoid having to go via a string):

XElement el = new XElement("foo",
     new XAttribute("abc","def"), new XElement("bar"));
var doc = new XmlDocument();
using (var reader = el.CreateReader()) {
    doc.Load(reader);
}
XmlNode node = doc.DocumentElement; // node could also be typed as XmlElement

and to convert it back again (to match the question title):

XElement andBackAgain;
using(var reader = new XmlNodeReader(node)) {
    andBackAgain = XElement.Load(reader);
}
Share:
11,824
Kioko Kiaza
Author by

Kioko Kiaza

I´m from Japan but i live in the Basque Country since 3 years far now... i work like software developer in a friends young company

Updated on June 14, 2022

Comments

  • Kioko Kiaza
    Kioko Kiaza almost 2 years

    I´m getting the error Calling the function GetListItems but is kind weird because it works in Visual Studio 2008 Express but no in Visual Basic 2010 Express:

     Dim ndQuery As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "")
    
        Dim ndViewFields As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "")
        Dim ndQueryOptions As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "")
    
        ndQueryOptions.InnerXml = "<IncludeMandatoryColumns>False</IncludeMandatoryColumns>" & _
                                    "<DateInUtc>True</DateInUtc>"
    
        ndViewFields.InnerXml = "<FieldRef Name=""LinkFilename"" />" & _
                                "<FieldRef Name=""Empresa"" />" & _
                                "<FieldRef Name=""Puesto"" />" & _
                                "<FieldRef Name=""Fecha_x0020_Vigente"" />" & _
                                "<FieldRef Name=""Oferta_x0020_vigente"" />"
    
        ndQuery.InnerXml = ""
    
        Try
    
            Dim ndListItems As XmlNode = IntranetLists.GetListItems(ListUUID, Nothing, _
                                                        ndQuery, ndViewFields, Nothing, ndQueryOptions, Nothing)
    

    And this is the function i´m calling:

    Public Function GetListItems(ByVal listName As String, ByVal viewName As String, ByVal query As System.Xml.Linq.XElement, ByVal viewFields As System.Xml.Linq.XElement, ByVal rowLimit As String, ByVal queryOptions As System.Xml.Linq.XElement, ByVal webID As String) As System.Xml.Linq.XElement
            Dim inValue As ListasIntranetGureak.GetListItemsRequest = New ListasIntranetGureak.GetListItemsRequest()
            inValue.Body = New ListasIntranetGureak.GetListItemsRequestBody()
            inValue.Body.listName = listName
            inValue.Body.viewName = viewName
            inValue.Body.query = query
            inValue.Body.viewFields = viewFields
            inValue.Body.rowLimit = rowLimit
            inValue.Body.queryOptions = queryOptions
            inValue.Body.webID = webID
            Dim retVal As ListasIntranetGureak.GetListItemsResponse = CType(Me,ListasIntranetGureak.ListsSoap).GetListItems(inValue)
            Return retVal.Body.GetListItemsResult
        End Function