473,993 Members | 44,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deserialize from SelectSingleNod e

Is it possible to pull a single node (element) out of a complex XML
file, using SelectSingleNod e(), 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 11699
"Geoff" <ge*********@gm ail.com> wrote in message news:db******** *************** ***@posting.goo gle.com...
Is it possible to pull a single node (element) out of a complex XML
file, using SelectSingleNod e(), 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.SelectSingl eNode( "//MyObject");
MyObject obj = serializer.Dese rialize( 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 "deserializ e" statement gives me the following error:

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

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

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

My code looks like this:

XmlDocument ResponseDocumen t = new XmlDocument();
ResponseDocumen t.Load @"C:\MyFile.xml ");
NamespaceManage r = new
XmlNamespaceMan ager(ResponseDo cument.NameTabl e);
NamespaceManage r.AddNamespace( "p",
"http://www.telcordia.c om/IDN/ELMS6");
XPathQuery = @"//p:FIELDED_ADDRE SS";
XmlNode AddressNode = ResponseDocumen t.SelectSingleN ode(XPathQuery,
NamespaceManage r);
if (AddressNode != null)
{
ALTERNATIVE_ADD RESSFIELDED_ADD RESS FieldedAddress;
XmlNodeReader NodeReader = new XmlNodeReader(A ddressNode);
XmlSerializer Serializer = new
XmlSerializer(t ypeof(ALTERNATI VE_ADDRESSFIELD ED_ADDRESS),
"http://www.telcordia.c om/IDN/ELMS6");
FieldedAddress = Serializer.Dese rialize(NodeRea der) as
ALTERNATIVE_ADD RESSFIELDED_ADD RESS;
}

My XML file looks like this:

<?xml version="1.0" ?>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<Object Id="pp">
<ADDRESS_VALIDA TION_OUTPUT
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.telcordia.c om/IDN/ELMS6"
version="ELMS6_ ADDRESS_VALIDAT ION_OUTPUT_v2-0"
xsi:schemaLocat ion="http://www.telcordia.c om/IDN/ELMS6
ADDRESS_VALIDAT ION_OUTPUT.xsd" >
<COMMON_QUERY_R SP></COMMON_QUERY_RS P>
<ALTERNATIVE_AD DRESS_INFORMATI ON_LIST>
<ALTERNATIVE_AD DRESS_INFORMATI ON>
<ALTERNATIVE_AD DRESS>
<FIELDED_ADDRES S>
<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_ADD RESS>
</ALTERNATIVE_ADD RESS_INFORMATIO N>
</ALTERNATIVE_ADD RESS_INFORMATIO N_LIST>
</ADDRESS_VALIDAT ION_OUTPUT>
</Object>
</Signature>
Nov 12 '05 #3
OK. I got this working finally! Cool. Thank you.

The Serializer.Dese rialize() 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.Seri alization.XmlTy peAttribute(Nam espace="http://www.telcordia.c om/IDN/ELMS6")]

[XmlRoot (ElementName="F IELDED_ADDRESS" )]

public class ADDRESS_VALIDAT ION_OUTPUTALTER NATIVE_ADDRESS_ INFORMATIONALTE RNATIVE_ADDRESS FIELDED_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******** *****@TK2MSFTNG P11.phx.gbl...
"Geoff" <ge*********@gm ail.com> wrote in message news:db******** *************** ***@posting.goo gle.com...
Is it possible to pull a single node (element) out of a complex XML
file, using SelectSingleNod e(), 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.SelectSingl eNode( "//MyObject");
MyObject obj = serializer.Dese rialize( 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
6070
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 xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance">
2
9877
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" xmlns="mailto:neo_in_matrix@msn.com?subject=ssmprjx" > <pr>
7
18571
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 http://GreenJAR/DAVCatalog/Dashboards/GreenJAR/Documents/Schemas/RiXML2.xsd" researchID="" createDateTime="2003-07-10T00:00:00" language="eng" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.rixml.org/2002/6/RIXML">
3
14889
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. Is this a known problem with XPath and SelectSingleNode? Code snippet: <snip>
4
4979
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 nunit test that exposes the problem. I have run the test under both the 1.0 and 1.1 framework with the same result. public void XPathBooks() { XmlDocument smallDoc = new XmlDocument();
7
3843
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", myXmlNSMgr).InnerText;
19
16297
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" xmlns:sns="http://www.test.org/sub" xmlns:mns="http://www.test.org/mini"> <data>
0
1823
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. .Load(pcXmlFile) oPackage = .documentElement.SelectSingleNode("//Package")
9
3309
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. What happens when the element is supplied as <fieldTag4000Field/(empty), it does not equate to null. I want to be able handle this at the deserialization level rahter than in my edits later.
0
10411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11929
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11497
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10998
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10164
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6510
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6669
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.