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

前端之家收集整理的这篇文章主要介绍了.net – 无法将System.Xml.XmlNode转换为System.Xml.Linq.XElement前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我收到错误调用函数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,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
我没有VB 2008方便,但据我所知,XElement和XmlNode之间从未进行任何自动转换(隐式或显式) – LINQ-to-XML主要是DOM的并行实现,只有一个很少的东西(比如XmlReader作为源代码)的共同点.

然而;在解决问题方面,我建议的最好的方法是使用xml;一个C#示例(使用CreateReader避免必须通过字符串):

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);
}

猜你在找的XML相关文章