How to pull single property out of xml file?

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

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.
 
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:

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.FormatterAssemblyStyle.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 said:
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]
- (e-mail address removed)

Dave said:
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.
 
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]
- (e-mail address removed)

Dave said:
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.FormatterAssemblyStyle.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

in
message news:[email protected]...
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]
- (e-mail address removed)

Dave said:
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.
 
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.FormatterAssemblyStyle.Simple;
s.Serialize(fs, xx);
fs.Close();
}
};
}

- Dave
Nicholas Paldino said:
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]
- (e-mail address removed)

Dave said:
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.FormatterAssemblyStyle.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

in
message news:[email protected]...
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]
- (e-mail address removed)

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.
 
Back
Top