Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 

Go Back   XSL - XML - RSS Forums > XML General > .NET and XML

Tags:



Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-25-2008, 01:40 PM
Martin Honnen
 
Posts: n/a

Default Re: Returned data from WebService



John Daly wrote:

> When I try to use the SelectSingleNode from the XPathNavigaotr object I get a
> NullRefernceException. The outerXML is disaplayed below:
>
> <GetFormDataResponse xmlns="http://brickmua.com/">
> <GetFormDataResult>
> <my:myFields xml:lang="en-us"
> xmlns:my="http://schemas.microsoft.com/office/info path/2003/myXSD/2008-11-20T14:46:22">
> <my:field1>Test Value 1</my:field1>
> <my:field2>Test Value 2</my:field2>
> <my:field3>Test Value 3</my:field3>
> </my:myFields>
> </GetFormDataResult>
> </GetFormDataResponse>
>
> Does anyone know the correct XPATH that I would need to get to the
> my:field1; my:field2 and my:field3 values.


Here is an example:

XPathDocument xDoc1 = new XPathDocument(@"..\..\XMLFile1.xml");
XPathNavigator nav = xDoc1.CreateNavigator();
XmlNamespaceManager mgr1 = new
XmlNamespaceManager(nav.NameTable);
mgr1.AddNamespace("df", "http://brickmua.com/");
mgr1.AddNamespace("my",
"http://schemas.microsoft.com/office/infopath/ 2003/myXSD/2008-11-20T14:46:22");
XPathNavigator myFields =
nav.SelectSingleNode("df:GetFormDataResponse/df:Ge tFormDataResult/my:myFields",
mgr1);
Console.WriteLine("field1: {0}; field2: {1}; field3: {2}",
myFields.SelectSingleNode("my:field1", mgr1).Value,
myFields.SelectSingleNode("my:field2", mgr1).Value,
myFields.SelectSingleNode("my:field3", mgr1).Value);

As you can see, an XmlNamespaceManager is used which binds a prefix
("df" in the example, choose what you like/prefer) to the default
namespace URI as XPath 1.0 requires that. Then that XmlNamespaceManager
is passed to any Select or SelectSingleNode call and the prefix is used
in the XPath expression.

If you wanted to use XmlDocument and SelectNodes/SelectSingleNode on
that API then you also need and XmlNamespaceManager and bind a prefix to
the default namespace that XML document declares.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-25-2008, 04:45 PM
Martin Honnen
 
Posts: n/a

Default Re: Returned data from WebService

John Daly wrote:
> Martin, Thanks for the code sample. I had tried that code, and am still
> recieving the NullReferenceArgument.


That is rather odd as that was tested code run against the XML you
posted. If you still want to solve that problem then provide the exact
code that gives the error.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Reply With Quote
  #3 (permalink)  
Old 11-25-2008, 04:45 PM
John Daly
 
Posts: n/a

Default Re: Returned data from WebService

Martin,
This is the code that use to get the XML out of the WebService.

Dim wsc As WebServiceConnection =
DirectCast(Me.DataConnections("GetFormData"), WebServiceConnection)
Dim xp As XPathNavigator = Me.MainDataSource.CreateNavigator
Dim idValue As String =
xp.SelectSingleNode("/my:myFields/my:field4", Me.NamespaceManager).Value

' Create XmlDocuments.
Dim inputDocument As XmlDocument = New XmlDocument()
Dim outputDocument As XmlDocument = New XmlDocument()
Dim errorsDocument As XmlDocument = New XmlDocument()

' Load input document.
inputDocument.LoadXml("<GetFormData
xmlns=""http://brickmua.com/""><id>" & idValue & "</id></GetFormData>")

' Create XPathNavigator objects for documents.
Dim inputNav As XPathNavigator = inputDocument.CreateNavigator()
Dim outputNav As XPathNavigator = outputDocument.CreateNavigator()
Dim errorsNav As XPathNavigator = errorsDocument.CreateNavigator()

' Call Execute method. - retrieves data from web service xml is
in the outputNav field
wsc.Execute(inputNav, outputNav, errorsNav)

the outputNav.OuterXML property contains the orginal xml that I posted.
outputNav is a XpathNavigator object so I assumed all I had to do was access
the data as follows:

Dim val1 As String =
outputNav.SelectSingleNode("GetFormDataResponse/Ge tFormDataResult/myFields/my:field1", namespace manager).Value

I also tried the following:
Dim val1 As String =
outputNav.SelectSingleNode("/GetFormDataResponse/G etFormDataResult/myFields/my:field1", namespace manager).Value

Also tried creating an XMLDocument from the OuterXML property - can create
and access the children can not access using XPATH expressions. Also tried to
us an XPathDocument and a XMLReader.

Again I thank you for ythe help, as this would be the proper way to do it
instead of a dataset.

Let me know if you need more info.

"Martin Honnen" wrote:

> John Daly wrote:
> > Martin, Thanks for the code sample. I had tried that code, and am still
> > recieving the NullReferenceArgument.

>
> That is rather odd as that was tested code run against the XML you
> posted. If you still want to solve that problem then provide the exact
> code that gives the error.
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Contact Us -|- XSL - XML - RSS Forums -|- Archive -|- Top -|-Rules/Disclaimer-|-Help/Support -|-Advertise
© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
All times are GMT. The time now is 05:33 PM.
Style Developed by Epic Designz