472,989 Members | 3,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

Deserialize from SelectSingleNode

Is it possible to pull a single node (element) out of a complex XML
file, using SelectSingleNode(), and then deserialize that node to an
object?

Could someone please post, or point me to, a simple, but complete
example?
Nov 12 '05 #1
5 11494
"Geoff" <ge*********@gmail.com> wrote in message news:db**************************@posting.google.c om...
Is it possible to pull a single node (element) out of a complex XML
file, using SelectSingleNode(), and then deserialize that node to an
object?


Yes, if your XML document looks like this, for example,

<MyDocument>
<ComplexInfo>
<DontCare SomeStatistic="55" />
</ComplexInfo>
<MyObject>
<Name>Geoff</Name>
</MyObject>
</MyDocument>

and you have a class to deserialize that looks like this,

public class MyObject
{
public string Name;
}

then you should have no problem selecting a single node, MyObject,
from the more complex document and then wrapping that node in an
XmlNodeReader to use for deserialization, like this,

XmlSerializer serializer = new XmlSerializer( typeof( MyObject));
XmlDocument doc = new XmlDocument( );
doc.Load( "myDocument.xml");

XmlNode node = doc.SelectSingleNode( "//MyObject");
MyObject obj = serializer.Deserialize( new XmlNodeReader( node)) as MyObject;
Derek Harmon
Nov 12 '05 #2
Derek, thank you so much for the excellent response. You are a
gentleman and a scholar. You have confirmed for me that my approach
should work, but I'm still having trouble, I think because of
namespaces declared in the XML file. Can you expand your example a
bit to account for a default namespace in the XML file? Or, can you
tell me what might be wrong with my code, below?

The "deserialize" statement gives me the following error:

There is an error in the XML document. --> &lt;FIELDED_ADDRESS
xmlns='http://www.telcordia.com/IDN/ELMS6'&gt; was not expected.

"FIELDED_ADDRESS" is the name of the node I'm trying to
select/deserialize.

"http://www.telcordia.com/IDN/ELMS6" is a namespace declared in the
XML file.

My code looks like this:

XmlDocument ResponseDocument = new XmlDocument();
ResponseDocument.Load @"C:\MyFile.xml");
NamespaceManager = new
XmlNamespaceManager(ResponseDocument.NameTable);
NamespaceManager.AddNamespace("p",
"http://www.telcordia.com/IDN/ELMS6");
XPathQuery = @"//p:FIELDED_ADDRESS";
XmlNode AddressNode = ResponseDocument.SelectSingleNode(XPathQuery,
NamespaceManager);
if (AddressNode != null)
{
ALTERNATIVE_ADDRESSFIELDED_ADDRESS FieldedAddress;
XmlNodeReader NodeReader = new XmlNodeReader(AddressNode);
XmlSerializer Serializer = new
XmlSerializer(typeof(ALTERNATIVE_ADDRESSFIELDED_AD DRESS),
"http://www.telcordia.com/IDN/ELMS6");
FieldedAddress = Serializer.Deserialize(NodeReader) as
ALTERNATIVE_ADDRESSFIELDED_ADDRESS;
}

My XML file looks like this:

<?xml version="1.0" ?>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<Object Id="pp">
<ADDRESS_VALIDATION_OUTPUT
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.telcordia.com/IDN/ELMS6"
version="ELMS6_ADDRESS_VALIDATION_OUTPUT_v2-0"
xsi:schemaLocation="http://www.telcordia.com/IDN/ELMS6
ADDRESS_VALIDATION_OUTPUT.xsd">
<COMMON_QUERY_RSP></COMMON_QUERY_RSP>
<ALTERNATIVE_ADDRESS_INFORMATION_LIST>
<ALTERNATIVE_ADDRESS_INFORMATION>
<ALTERNATIVE_ADDRESS>
<FIELDED_ADDRESS>
<SANO>1445</SANO>
<SATH>DR</SATH>
<SASN>MONROE</SASN>
<SASS>NE</SASS>
<LD3>APT</LD3>
<LV3>C-27</LV3>
<CITY>ATLANTA</CITY>
<STATE>GA</STATE>
</FIELDED_ADDRESS>
</ALTERNATIVE_ADDRESS>
</ALTERNATIVE_ADDRESS_INFORMATION>
</ALTERNATIVE_ADDRESS_INFORMATION_LIST>
</ADDRESS_VALIDATION_OUTPUT>
</Object>
</Signature>
Nov 12 '05 #3
OK. I got this working finally! Cool. Thank you.

The Serializer.Deserialize() method kept giving me "XXX was not
expected."

I thought it was talking about the namespace for XXX, but it was
actually complaining about the entire element. Why?... Apparently
because my class name didn't exactly match the element in the XML
file.

Unfortunately, my class name was generated from an XSD file for the
XML file I'm working with. The class name has the form AAABBBCCC,
where the actual element I'm interested in, CCC, is nested under
elements AAA and BBB. I really don't want to change it, to match the
element, because if the XSD changes in the future, I will use the
xsd.exe tool to regenerate the classes, and any manual changes I've
made will get blasted.

Maybe there is an option for xsd.exe to generate class names which
match the element names, or maybe there is a way to do the
Deserialize() without the names matching. ???
Nov 12 '05 #4
I was able to use the XmlRoot attribute to map the class name to the
element name in the XML file, so at least now I won't have to actually
change the xsd.exe generated classes themselves. Only issue now is
I'll have to add the attribute any time the XSD file changes. Not
such a big deal. Example below:
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="http://www.telcordia.com/IDN/ELMS6")]

[XmlRoot (ElementName="FIELDED_ADDRESS")]

public class ADDRESS_VALIDATION_OUTPUTALTERNATIVE_ADDRESS_INFOR MATIONALTERNATIVE_ADDRESSFIELDED_ADDRESS
Nov 12 '05 #5
Thanks Derek. Just curious if Deserializing the whole thing with
XmlSerializer to get the MyObject string and using that to deserialize the
string xml would be faster or slower? Naturally you would need to define
MyDocument in a class first. Does not XmlDocument deserializing the whole
thing also just to find the MyObject tag? TIA

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Derek Harmon" <lo*******@msn.com> wrote in message
news:OH*************@TK2MSFTNGP11.phx.gbl...
"Geoff" <ge*********@gmail.com> wrote in message news:db**************************@posting.google.c om...
Is it possible to pull a single node (element) out of a complex XML
file, using SelectSingleNode(), and then deserialize that node to an
object?


Yes, if your XML document looks like this, for example,

<MyDocument>
<ComplexInfo>
<DontCare SomeStatistic="55" />
</ComplexInfo>
<MyObject>
<Name>Geoff</Name>
</MyObject>
</MyDocument>

and you have a class to deserialize that looks like this,

public class MyObject
{
public string Name;
}

then you should have no problem selecting a single node, MyObject,
from the more complex document and then wrapping that node in an
XmlNodeReader to use for deserialization, like this,

XmlSerializer serializer = new XmlSerializer( typeof( MyObject));
XmlDocument doc = new XmlDocument( );
doc.Load( "myDocument.xml");

XmlNode node = doc.SelectSingleNode( "//MyObject");
MyObject obj = serializer.Deserialize( new XmlNodeReader( node)) as

MyObject;

Derek Harmon


Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John R. | last post by:
I have an application written in C# and i am using MS XML DOM! I have a document with the following structure (only the <DicEntry> - Elements are important): <NewDataSet...
2
by: Edward Yang | last post by:
My XML document has a default namespace specified by xmlns="some_url". Here it is: <?xml version="1.0" encoding="utf-8" ?> <ssmproject name="sample" server="sql"...
7
by: Jason | last post by:
Hi I have an XML file i need to load and read the contents. Here is the top part of the xml file. <Research xsi:schemaLocation="http://www.rixml.org/2002/6/RIXML...
3
by: muscha | last post by:
Hi All, I have this weird problem. I have an xml document and tried to do an XPath query to it. If I use the SelectSingleNode method it throws an exception but it works with SelectNodes method....
4
by: Rune | last post by:
I have two queries that appear to be exactly the same, but one of them returns null while the other one returns a valid result! Can anyone provide an explanation to why this is so? Below is an...
7
by: Sashi | last post by:
Two questions: (1) I can pull the text of an XML element as a string just fine using code as such: strSomeString = myXmlDoc.SelectSingleNode("/Element1/Element2/Element3",...
19
by: David Thielen | last post by:
Hi; If there are no namespaces this works fine for me. But if the xml has namespaces, then I get either no node back or an exception. Here is the sample xml: <root xmlns="http://www.test.org"...
0
by: compumate99 | last post by:
I am trying to parse the xml document using selectsinglenode method. I am doing this using Visual Foxpro >>> loResultXml = CreateObject("Microsoft.XMLDOM") With loResultXml .Async = .F. ...
9
by: =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?= | last post by:
Hi, I have a schema that has an optional element, fieldTag4000Field. If the element is omitted from the XML request, when it is deserialized, it will be null when I check it - which is fine. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.