473,320 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 11598
"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. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.