By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,695 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,695 IT Pros & Developers. It's quick & easy.

How to pull single property out of xml file?

P: n/a
I am saving a class by giving it the "Serializable" attribute, and calling
Serialize. Is there a way that I can pull the value of an individual member
of the class, out of the resulting xml file, without constructing a new
instance of the class deserializing the file into it? In this case, I just
need the value of a string member, and don't want to construct the entire
class to get it.
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Dave,

The xml that the XmlSerializer generates for a particular type should be
pretty consistent. I would say to take a look at the output that the type
that you are serializing generates, and then construct an XPath query that
you can use to get the value.

For primitive value types, this should be fine, but for propreties that
return class instances, you might have no other option than to run the
XmlSerializer on the whole thing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I am saving a class by giving it the "Serializable" attribute, and calling
Serialize. Is there a way that I can pull the value of an individual
member
of the class, out of the resulting xml file, without constructing a new
instance of the class deserializing the file into it? In this case, I
just
need the value of a string member, and don't want to construct the entire
class to get it.

Nov 16 '05 #2

P: n/a
Nicholas:

Thanks for the reply, but I guess I forgot to mention that I'm new to this
stuff. "Xpath query"? How do I do that? Maybe a simple example would make
my question more clear:

Suppose I have a class as follows:

[Serializable] public class foo {
public string member1;
public string member2;
};

And, I serialize it as follows:

....
foo xx = new foo();
xx.member1 = "test value";
xx.member2 = "test value2";
FileStream fs = new FileStream("test.xml", FileMode.Create);
SoapFormatter s = new SoapFormatter();
s.AssemblyFormat =
System.Runtime.Serialization.Formatters.FormatterA ssemblyStyle.Simple;
s.Serialize(fs, foo);
fs.Close();

So, now I have a file, "test.xml", and I want to pull out the value of
"member1", without deserializing the file into a new instance of foo. How
do I do that?

- Dave

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eQ**************@TK2MSFTNGP14.phx.gbl...
Dave,

The xml that the XmlSerializer generates for a particular type should be pretty consistent. I would say to take a look at the output that the type
that you are serializing generates, and then construct an XPath query that
you can use to get the value.

For primitive value types, this should be fine, but for propreties that return class instances, you might have no other option than to run the
XmlSerializer on the whole thing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I am saving a class by giving it the "Serializable" attribute, and calling Serialize. Is there a way that I can pull the value of an individual
member
of the class, out of the resulting xml file, without constructing a new
instance of the class deserializing the file into it? In this case, I
just
need the value of a string member, and don't want to construct the entire class to get it.


Nov 16 '05 #3

P: n/a
Dave,

Can you post test.xml? Then, we can see the structure, and tell you how
to get the value.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:uv**************@TK2MSFTNGP14.phx.gbl...
Nicholas:

Thanks for the reply, but I guess I forgot to mention that I'm new to this
stuff. "Xpath query"? How do I do that? Maybe a simple example would
make
my question more clear:

Suppose I have a class as follows:

[Serializable] public class foo {
public string member1;
public string member2;
};

And, I serialize it as follows:

...
foo xx = new foo();
xx.member1 = "test value";
xx.member2 = "test value2";
FileStream fs = new FileStream("test.xml", FileMode.Create);
SoapFormatter s = new SoapFormatter();
s.AssemblyFormat =
System.Runtime.Serialization.Formatters.FormatterA ssemblyStyle.Simple;
s.Serialize(fs, foo);
fs.Close();

So, now I have a file, "test.xml", and I want to pull out the value of
"member1", without deserializing the file into a new instance of foo. How
do I do that?

- Dave

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:eQ**************@TK2MSFTNGP14.phx.gbl...
Dave,

The xml that the XmlSerializer generates for a particular type should

be
pretty consistent. I would say to take a look at the output that the
type
that you are serializing generates, and then construct an XPath query
that
you can use to get the value.

For primitive value types, this should be fine, but for propreties

that
return class instances, you might have no other option than to run the
XmlSerializer on the whole thing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
>I am saving a class by giving it the "Serializable" attribute, and calling > Serialize. Is there a way that I can pull the value of an individual
> member
> of the class, out of the resulting xml file, without constructing a new
> instance of the class deserializing the file into it? In this case, I
> just
> need the value of a string member, and don't want to construct the entire > class to get it.
>
>



Nov 16 '05 #4

P: n/a
Ok. My actual class is more complex, but if you can explain how to do
it with this test code, I think I can figure out the rest. Here's the xml
file produced:

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:foo id="ref-1"
xmlns:a1="http://schemas.microsoft.com/clr/nsassem/XMLTest2/XMLTest2">
<member1 id="ref-3">test value</member1>
<member2 id="ref-4">test value2</member2>
</a1:foo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Here's the code I used to produce the above:

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
namespace XMLTest
{
[Serializable] public class foo {
public string member1;
public string member2;
static void Main(string[] args)
{
foo xx = new foo();
xx.member1 = "test value";
xx.member2 = "test value2";
FileStream fs = new FileStream(@"c:\test.xml",
FileMode.Create);
SoapFormatter s = new SoapFormatter();
s.AssemblyFormat =

System.Runtime.Serialization.Formatters.FormatterA ssemblyStyle.Simple;
s.Serialize(fs, xx);
fs.Close();
}
};
}

- Dave
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:<uz**************@TK2MSFTNGP11.phx.gbl>...
Dave,

Can you post test.xml? Then, we can see the structure, and tell you how to get the value.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:uv**************@TK2MSFTNGP14.phx.gbl...
Nicholas:

Thanks for the reply, but I guess I forgot to mention that I'm new to this stuff. "Xpath query"? How do I do that? Maybe a simple example would make
my question more clear:

Suppose I have a class as follows:

[Serializable] public class foo {
public string member1;
public string member2;
};

And, I serialize it as follows:

...
foo xx = new foo();
xx.member1 = "test value";
xx.member2 = "test value2";
FileStream fs = new FileStream("test.xml", FileMode.Create);
SoapFormatter s = new SoapFormatter();
s.AssemblyFormat =
System.Runtime.Serialization.Formatters.FormatterA ssemblyStyle.Simple; s.Serialize(fs, foo);
fs.Close();

So, now I have a file, "test.xml", and I want to pull out the value of "member1", without deserializing the file into a new instance of foo. How do I do that?

- Dave

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eQ**************@TK2MSFTNGP14.phx.gbl...
Dave,

The xml that the XmlSerializer generates for a particular type should
be
pretty consistent. I would say to take a look at the output that
the type
that you are serializing generates, and then construct an XPath query that
you can use to get the value.

For primitive value types, this should be fine, but for propreties that
return class instances, you might have no other option than to run
the XmlSerializer on the whole thing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
>I am saving a class by giving it the "Serializable" attribute, and

calling
> Serialize. Is there a way that I can pull the value of an individual > member
> of the class, out of the resulting xml file, without constructing a new > instance of the class deserializing the file into it? In this case, I > just
> need the value of a string member, and don't want to construct

the entire
> class to get it.
>
>



Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.