无法将System.Xml.XmlNode转换为System.Xml.Linq.XElement

时间:2010-11-24 07:51:31

标签: .net vb.net visual-studio-2010

我收到错误调用函数GetListItems但是有点奇怪,因为它在Visual Studio 2008 Express中有效但在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)

这就是我所说的功能:

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

1 个答案:

答案 0 :(得分:9)

我没有VB 2008方便,但据我所知,从未XElementXmlNode之间进行任何自动转换(隐式或显式) - LINQ-to-XML主要是DOM的并行实现,只有少数东西(如XmlReader作为源)。

然而;在解决问题方面,我建议的最好的方法是使用xml;一个C#示例(使用CreateReader来避免必须通过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

并再次将其转换回来(以匹配问题标题):

XElement andBackAgain;
using(var reader = new XmlNodeReader(node)) {
    andBackAgain = XElement.Load(reader);
}