|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Error using XMLTextReader to read XMLDocumentparameter to one of the methods. I would like to use the XMLTextReader to read the XML but I am getting the following error: Value of type System.xml.xmldocument cannot be converted to System.IO.textreader. I would think this is possible to do. Code snippet is below: ------------------------------------------------------- Public Function ChangeAddress(ByVal xml As XmlDocument) As Boolean Try Dim xtr As New XmlTextReader(xml) ------------------------------------------------------- Any and all help is greatfully appreciated. -- Thanks, Scott Hi Scott
You could do this (code follows), but I'm not sure why you would want to...? Essentially, the XmlDocument, (xml in your method) is an in memory tree of your XML data - the code below serializes the tree to a string, then reads it in again...it's kind of wasteful. If you want to access content of the document, then use methods like SelectNodes, GetElementsByTagName, and so on to read through the data in memory... Nigel Code: Public Function ChangeAddress(ByVal xml As XmlDocument) Dim sr As New IO.StringReader(xml.OuterXml) Dim t As New Xml.XmlTextReader(sr) While t.Read MessageBox.Show(t.Name) End While End Function Show quote "SQLScott" wrote: > I have a Web Service in which I am trying to pass an XMLDocument as a > parameter to one of the methods. I would like to use the XMLTextReader to > read the XML but I am getting the following error: > > Value of type System.xml.xmldocument cannot be converted to > System.IO.textreader. > > I would think this is possible to do. > > Code snippet is below: > > ------------------------------------------------------- > Public Function ChangeAddress(ByVal xml As > XmlDocument) As Boolean > > Try > > Dim xtr As New XmlTextReader(xml) > > ------------------------------------------------------- > > Any and all help is greatfully appreciated. > > -- > Thanks, > > Scott Thanks Nigel, I appreciate the response.
You state "why would you want to?". Is there a better of doing what I am tryign to accomplish? Should I not be passing an XMLDocument? Scott Show quote "Nigel Armstrong" wrote: > Hi Scott > > You could do this (code follows), but I'm not sure why you would want to...? > > Essentially, the XmlDocument, (xml in your method) is an in memory tree of > your XML data - the code below serializes the tree to a string, then reads it > in again...it's kind of wasteful. If you want to access content of the > document, then use methods like SelectNodes, GetElementsByTagName, and so on > to read through the data in memory... > > Nigel > Code: > Public Function ChangeAddress(ByVal xml As XmlDocument) > Dim sr As New IO.StringReader(xml.OuterXml) > Dim t As New Xml.XmlTextReader(sr) > While t.Read > MessageBox.Show(t.Name) > End While > End Function > > "SQLScott" wrote: > > > I have a Web Service in which I am trying to pass an XMLDocument as a > > parameter to one of the methods. I would like to use the XMLTextReader to > > read the XML but I am getting the following error: > > > > Value of type System.xml.xmldocument cannot be converted to > > System.IO.textreader. > > > > I would think this is possible to do. > > > > Code snippet is below: > > > > ------------------------------------------------------- > > Public Function ChangeAddress(ByVal xml As > > XmlDocument) As Boolean > > > > Try > > > > Dim xtr As New XmlTextReader(xml) > > > > ------------------------------------------------------- > > > > Any and all help is greatfully appreciated. > > > > -- > > Thanks, > > > > Scott Hi Scott
I don't have a problem with you passing an XmlDocument via a Web service! It's just that with your XmlDocument parameter on your Web service method, you already have a programmatic interface to your XML data - there's no need to reparse it with the XmlTextReader...so as I mentioned in my previous post, you can use GetElementsByTagName(), SelectNodes() and so on to get access to the data, you don't need to reparse. Nigel Armstrong Show quote "SQLScott" wrote: > Thanks Nigel, I appreciate the response. > > You state "why would you want to?". Is there a better of doing what I am > tryign to accomplish? Should I not be passing an XMLDocument? > > Scott > > "Nigel Armstrong" wrote: > > > Hi Scott > > > > You could do this (code follows), but I'm not sure why you would want to...? > > > > Essentially, the XmlDocument, (xml in your method) is an in memory tree of > > your XML data - the code below serializes the tree to a string, then reads it > > in again...it's kind of wasteful. If you want to access content of the > > document, then use methods like SelectNodes, GetElementsByTagName, and so on > > to read through the data in memory... > > > > Nigel > > Code: > > Public Function ChangeAddress(ByVal xml As XmlDocument) > > Dim sr As New IO.StringReader(xml.OuterXml) > > Dim t As New Xml.XmlTextReader(sr) > > While t.Read > > MessageBox.Show(t.Name) > > End While > > End Function > > > > "SQLScott" wrote: > > > > > I have a Web Service in which I am trying to pass an XMLDocument as a > > > parameter to one of the methods. I would like to use the XMLTextReader to > > > read the XML but I am getting the following error: > > > > > > Value of type System.xml.xmldocument cannot be converted to > > > System.IO.textreader. > > > > > > I would think this is possible to do. > > > > > > Code snippet is below: > > > > > > ------------------------------------------------------- > > > Public Function ChangeAddress(ByVal xml As > > > XmlDocument) As Boolean > > > > > > Try > > > > > > Dim xtr As New XmlTextReader(xml) > > > > > > ------------------------------------------------------- > > > > > > Any and all help is greatfully appreciated. > > > > > > -- > > > Thanks, > > > > > > Scott Ah, ok, i'm with you now. It makes sense now that I have gone back to
re-read your original answer. Sorry for the confusion. Scott Show quote "Nigel Armstrong" wrote: > Hi Scott > > I don't have a problem with you passing an XmlDocument via a Web service! > It's just that with your XmlDocument parameter on your Web service method, > you already have a programmatic interface to your XML data - there's no need > to reparse it with the XmlTextReader...so as I mentioned in my previous post, > you can use GetElementsByTagName(), SelectNodes() and so on to get access to > the data, you don't need to reparse. > > Nigel Armstrong > > "SQLScott" wrote: > > > Thanks Nigel, I appreciate the response. > > > > You state "why would you want to?". Is there a better of doing what I am > > tryign to accomplish? Should I not be passing an XMLDocument? > > > > Scott > > > > "Nigel Armstrong" wrote: > > > > > Hi Scott > > > > > > You could do this (code follows), but I'm not sure why you would want to...? > > > > > > Essentially, the XmlDocument, (xml in your method) is an in memory tree of > > > your XML data - the code below serializes the tree to a string, then reads it > > > in again...it's kind of wasteful. If you want to access content of the > > > document, then use methods like SelectNodes, GetElementsByTagName, and so on > > > to read through the data in memory... > > > > > > Nigel > > > Code: > > > Public Function ChangeAddress(ByVal xml As XmlDocument) > > > Dim sr As New IO.StringReader(xml.OuterXml) > > > Dim t As New Xml.XmlTextReader(sr) > > > While t.Read > > > MessageBox.Show(t.Name) > > > End While > > > End Function > > > > > > "SQLScott" wrote: > > > > > > > I have a Web Service in which I am trying to pass an XMLDocument as a > > > > parameter to one of the methods. I would like to use the XMLTextReader to > > > > read the XML but I am getting the following error: > > > > > > > > Value of type System.xml.xmldocument cannot be converted to > > > > System.IO.textreader. > > > > > > > > I would think this is possible to do. > > > > > > > > Code snippet is below: > > > > > > > > ------------------------------------------------------- > > > > Public Function ChangeAddress(ByVal xml As > > > > XmlDocument) As Boolean > > > > > > > > Try > > > > > > > > Dim xtr As New XmlTextReader(xml) > > > > > > > > ------------------------------------------------------- > > > > > > > > Any and all help is greatfully appreciated. > > > > > > > > -- > > > > Thanks, > > > > > > > > Scott Nigel, quick question. When I write the code to call the method adn pass it
an XMLDocument, the intellisense displays the parameter as: system.xml.xmlnode IN other words, bReturn = ws.CallMethod(xml as system.xml.xmlnode) Even though the parameter on the method is ByVal xml as XMLDocument. I am at a loss. Any ideas why? Many thanks for your help on this. Show quote "Nigel Armstrong" wrote: > Hi Scott > > I don't have a problem with you passing an XmlDocument via a Web service! > It's just that with your XmlDocument parameter on your Web service method, > you already have a programmatic interface to your XML data - there's no need > to reparse it with the XmlTextReader...so as I mentioned in my previous post, > you can use GetElementsByTagName(), SelectNodes() and so on to get access to > the data, you don't need to reparse. > > Nigel Armstrong > > "SQLScott" wrote: > > > Thanks Nigel, I appreciate the response. > > > > You state "why would you want to?". Is there a better of doing what I am > > tryign to accomplish? Should I not be passing an XMLDocument? > > > > Scott > > > > "Nigel Armstrong" wrote: > > > > > Hi Scott > > > > > > You could do this (code follows), but I'm not sure why you would want to...? > > > > > > Essentially, the XmlDocument, (xml in your method) is an in memory tree of > > > your XML data - the code below serializes the tree to a string, then reads it > > > in again...it's kind of wasteful. If you want to access content of the > > > document, then use methods like SelectNodes, GetElementsByTagName, and so on > > > to read through the data in memory... > > > > > > Nigel > > > Code: > > > Public Function ChangeAddress(ByVal xml As XmlDocument) > > > Dim sr As New IO.StringReader(xml.OuterXml) > > > Dim t As New Xml.XmlTextReader(sr) > > > While t.Read > > > MessageBox.Show(t.Name) > > > End While > > > End Function > > > > > > "SQLScott" wrote: > > > > > > > I have a Web Service in which I am trying to pass an XMLDocument as a > > > > parameter to one of the methods. I would like to use the XMLTextReader to > > > > read the XML but I am getting the following error: > > > > > > > > Value of type System.xml.xmldocument cannot be converted to > > > > System.IO.textreader. > > > > > > > > I would think this is possible to do. > > > > > > > > Code snippet is below: > > > > > > > > ------------------------------------------------------- > > > > Public Function ChangeAddress(ByVal xml As > > > > XmlDocument) As Boolean > > > > > > > > Try > > > > > > > > Dim xtr As New XmlTextReader(xml) > > > > > > > > ------------------------------------------------------- > > > > > > > > Any and all help is greatfully appreciated. > > > > > > > > -- > > > > Thanks, > > > > > > > > Scott Scott
In practice it won't matter! The node that you get is actually a System.Xml.XmlElement - this code illustrates: Dim s As New localhost.XmlService Dim n As System.Xml.XmlNode = s.HelloWorld MessageBox.Show(n.NodeType & " " & n.OuterXml) What do you need to do with the returned data?? HTH Nigel Show quote "SQLScott" wrote: > Nigel, quick question. When I write the code to call the method adn pass it > an XMLDocument, the intellisense displays the parameter as: > > system.xml.xmlnode > > IN other words, bReturn = ws.CallMethod(xml as system.xml.xmlnode) > > Even though the parameter on the method is ByVal xml as XMLDocument. > > I am at a loss. Any ideas why? Many thanks for your help on this. > > "Nigel Armstrong" wrote: > > > Hi Scott > > > > I don't have a problem with you passing an XmlDocument via a Web service! > > It's just that with your XmlDocument parameter on your Web service method, > > you already have a programmatic interface to your XML data - there's no need > > to reparse it with the XmlTextReader...so as I mentioned in my previous post, > > you can use GetElementsByTagName(), SelectNodes() and so on to get access to > > the data, you don't need to reparse. > > > > Nigel Armstrong > > > > "SQLScott" wrote: > > > > > Thanks Nigel, I appreciate the response. > > > > > > You state "why would you want to?". Is there a better of doing what I am > > > tryign to accomplish? Should I not be passing an XMLDocument? > > > > > > Scott > > > > > > "Nigel Armstrong" wrote: > > > > > > > Hi Scott > > > > > > > > You could do this (code follows), but I'm not sure why you would want to...? > > > > > > > > Essentially, the XmlDocument, (xml in your method) is an in memory tree of > > > > your XML data - the code below serializes the tree to a string, then reads it > > > > in again...it's kind of wasteful. If you want to access content of the > > > > document, then use methods like SelectNodes, GetElementsByTagName, and so on > > > > to read through the data in memory... > > > > > > > > Nigel > > > > Code: > > > > Public Function ChangeAddress(ByVal xml As XmlDocument) > > > > Dim sr As New IO.StringReader(xml.OuterXml) > > > > Dim t As New Xml.XmlTextReader(sr) > > > > While t.Read > > > > MessageBox.Show(t.Name) > > > > End While > > > > End Function > > > > > > > > "SQLScott" wrote: > > > > > > > > > I have a Web Service in which I am trying to pass an XMLDocument as a > > > > > parameter to one of the methods. I would like to use the XMLTextReader to > > > > > read the XML but I am getting the following error: > > > > > > > > > > Value of type System.xml.xmldocument cannot be converted to > > > > > System.IO.textreader. > > > > > > > > > > I would think this is possible to do. > > > > > > > > > > Code snippet is below: > > > > > > > > > > ------------------------------------------------------- > > > > > Public Function ChangeAddress(ByVal xml As > > > > > XmlDocument) As Boolean > > > > > > > > > > Try > > > > > > > > > > Dim xtr As New XmlTextReader(xml) > > > > > > > > > > ------------------------------------------------------- > > > > > > > > > > Any and all help is greatfully appreciated. > > > > > > > > > > -- > > > > > Thanks, > > > > > > > > > > Scott That is completely correct.
If you are working with Web services, you need to think about the contract (WSDL) that is the basis for clients consuming your Web service. ASP.NET makes working with WSDL both easy and makes this hard by automatically generating WSDL for you based on XML Serialization and various attributes in your code. Easy because often you don't have to do anything to make the right (or a reasonable) thing happen. Hard because you still have to think about the WSDL that ASP.NET is generating, since that is all the client sees. It is important to note that Web services have *nothing* to do with objects and object-orientation. Web services pass XML data, not objects, not types. Both System.Xml.XmlNode and System.Xml.XmlDocument serialize down in your Web service message to an arbritrary, unconstrained XML element, represented in the WSDL as the XSD type <xs:any/>. Later, when you add a Web reference to a project, Visual Studio takes the WSDL for the Web service and creates proxy classes for the types represented in the WSDL. In other words, the proxy classes map XML data into classes. When a proxy classes are generated for a WSDL document, <xs:any/> maps to XmlNode. Consider your Web service from the perspective of clients that consume your Web service using different platforms, languages or vendors, say a J2EE client on a Sun or IBM system. They can consume an XML element (<xs:any/>) in your SOAP message, but that don't know anything about .NET Framework classes like XmlNode or XmlDocument. So you have to see beyond the ASP.NET-ism of thinking you are passing an XmlDocument on a Web service endpoint: you are passing what the WSDL says you are passing, and that's XML data. Cheers, Stuart Celarier, Fern Creek |
|||||||||||||||||||||||