Serializing / Deserializing XML using System.Xml.Serialization

V

VRSki

I am trying to create such class that could be serialized/deserialized
to/from an XML. I have chosen to use System.Xml.Serialization.XmlSerializer
serializer for that.

There is a caveat though. I would like to have one of my class's members to
be a string, which would contain an XML text. Here is what I mean.

My XML file:

<?xml version="1.0" encoding="utf-8" ?>
<RootElement>
<Child1 Name="1"/>
<Child2>
<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>
</Child2>
</RootElement>

I would like to be able to deserialize my RootElement and by refering to
Child2 be able to access the XML string
"<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>".

Here is what I have done so far:


[Serializable]
public class Child1
{
private string m_szName;

public Child1()
{
}

[XmlAttribute("Name")]
public String Name
{
get { return m_szName; }
set { m_szName = value; }
}
}

[Serializable]
public class Child2
{
private string m_szXml;

public Child1()
{
}

[XmlAttribute("Name")]
public String Xml
{
get { return m_szXml; }
set { m_szXml= value; }
}
}
[Serializable]
public class RootElement
{
private Child1 m_oChild1;
private Child2 m_oChild2;

public RootElement()
{
}

[XmlElement("Child1")]
public Child1 Child1
{
get { return m_oChild1; }
set { m_oChild1 = value; }
}

[XmlElement("Child2")]
public Child2 Child2
{
get { return m_oChild2; }
set { m_oChild2 = value; }
}
}


class Program
{
static void Main(string[] args)
{

System.IO.Stream oXmlStream = null;
oXmlStream =
System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("Test.Sample.xml");


System.Xml.Serialization.XmlSerializer oSerializer =
new
System.Xml.Serialization.XmlSerializer(typeof(RootElement));

RootElement oRootElement = oSerializer.Deserialize(oXmlStream )
as RootElement;

// String szInitialization = oRootElement.ChildRaw.Raw;
String szXml= oRootElement.Child2.Xml;

// ??? here I would like my szXml to
// ??? be "<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>"
}
}

Any ideas? Please help. I am trying to avoid to parse the XML by hand.

As a background... The reason I am looking to implement this is because in
the architecture I am developing, the main application may not know about the
classes (plug-in components) that could be serialized from the main XML
configuration file. I would like to be able to simply extract XML branches
from the main configuration and pass it on to the plug-in components that
would know what to do with it.

Thank you,
Vic
 
I

Ian McCaul

I am trying to create such class that could be serialized/deserialized
to/from an XML. I have chosen to use System.Xml.Serialization.XmlSerializer
serializer for that.

There is a caveat though. I would like to have one of my class's members to
be a string, which would contain an XML text. Here is what I mean.

My XML file:

<?xml version="1.0" encoding="utf-8" ?>
<RootElement>
<Child1 Name="1"/>
<Child2>
<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>
</Child2>
</RootElement>

I would like to be able to deserialize my RootElement and by refering to
Child2 be able to access the XML string
"<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>".

Here is what I have done so far:


[Serializable]
public class Child1
{
private string m_szName;

public Child1()
{
}

[XmlAttribute("Name")]
public String Name
{
get { return m_szName; }
set { m_szName = value; }
}
}

[Serializable]
public class Child2
{
private string m_szXml;

public Child1()
{
}

[XmlAttribute("Name")]
public String Xml
{
get { return m_szXml; }
set { m_szXml= value; }
}
}
[Serializable]
public class RootElement
{
private Child1 m_oChild1;
private Child2 m_oChild2;

public RootElement()
{
}

[XmlElement("Child1")]
public Child1 Child1
{
get { return m_oChild1; }
set { m_oChild1 = value; }
}

[XmlElement("Child2")]
public Child2 Child2
{
get { return m_oChild2; }
set { m_oChild2 = value; }
}
}


class Program
{
static void Main(string[] args)
{

System.IO.Stream oXmlStream = null;
oXmlStream =
System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("Test.Sample.xml");


System.Xml.Serialization.XmlSerializer oSerializer =
new
System.Xml.Serialization.XmlSerializer(typeof(RootElement));

RootElement oRootElement = oSerializer.Deserialize(oXmlStream )
as RootElement;

// String szInitialization = oRootElement.ChildRaw.Raw;
String szXml= oRootElement.Child2.Xml;

// ??? here I would like my szXml to
// ??? be "<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>"
}
}

Any ideas? Please help. I am trying to avoid to parse the XML by hand.

As a background... The reason I am looking to implement this is because in
the architecture I am developing, the main application may not know about the
classes (plug-in components) that could be serialized from the main XML
configuration file. I would like to be able to simply extract XML branches
from the main configuration and pass it on to the plug-in components that
would know what to do with it.

Thank you,
Vic

I think it may be a CDATA thing... not sure. But whenever I do serializing
like this i create the classes like you did, set their properties, then
serialize it into xml to see what the output would be so I know specifically
how to modify the xml to get a desired effect. So in your case create a test
or console project create an instance of the class, set the property to some
xml string you want to use and serialize it to see how it is saved off.
 
V

VRSki

Pete,

Thank you for your post. I do see the order of tags incorrect in my email,
but I assure you it is the problem in this post only. I did type XML snippet
by hand. I do know that my deserializer processes the XML without any errors.

I also understand that by serializing my object I would get the XML
representation of it which ultimately could be de-serialized. What I am
trying to do, though, is to write such code (using [Xml...] attributes and
possibly overriding attributes) that could deserialize that specific XML that
I provided the way I expect it. Which is to have a string member of a class
return its inner XML.

Is that possible?

Peter Duniho said:
I am trying to create such class that could be serialized/deserialized
to/from an XML. I have chosen to use
System.Xml.Serialization.XmlSerializer
serializer for that.

There is a caveat though. I would like to have one of my class's members
to
be a string, which would contain an XML text. Here is what I mean.

My XML file:

<?xml version="1.0" encoding="utf-8" ?>
<RootElement>
<Child1 Name="1"/>
<Child2>
<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>
</Child2>
</RootElement>

I would like to be able to deserialize my RootElement and by refering to
Child2 be able to access the XML string
"<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>". [...]

If you're going to deserialize using XmlSerializer, you really should have
serialized the data using XmlSerializer in the first place. It's not
inconceivable that you could write appropriate XML by hand, but using the
XmlSerializer for both directions will be _much_ more reliable.

The XML you posted isn't valid, so certainly as it stands that would
present some problems, even if you'd matched the correct format for what
XmlSerializer would want. But in terms of getting arbitrary strings
stored in an XML-serialized class, as long as you go through
XmlSerializer, it will escape anything that could be interpreted as part
of XML, so ensure that it's recovered strictly as a string and not
interpreted as XML during deserialization.

So, had you used XmlSerializer to serialize the data in the first place,
the fact that your string is XML, and invalid XML at that, would not be an
issue at all. I recommend you take that approach.

Pete
 
V

VRSki

Ian,

Thanks for your post. I think you are correct about CDATA. At the same time,
I am trying to de-serialize this specific XML. Which means, instead of making
changes to the XML, I'd like to change the code so that it would process the
XML the way I needed: to have a string member of a class return its inner XML.

Is that possible?

Thanks,
Vic

Ian McCaul said:
I am trying to create such class that could be serialized/deserialized
to/from an XML. I have chosen to use System.Xml.Serialization.XmlSerializer
serializer for that.

There is a caveat though. I would like to have one of my class's members to
be a string, which would contain an XML text. Here is what I mean.

My XML file:

<?xml version="1.0" encoding="utf-8" ?>
<RootElement>
<Child1 Name="1"/>
<Child2>
<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>
</Child2>
</RootElement>

I would like to be able to deserialize my RootElement and by refering to
Child2 be able to access the XML string
"<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>".

Here is what I have done so far:


[Serializable]
public class Child1
{
private string m_szName;

public Child1()
{
}

[XmlAttribute("Name")]
public String Name
{
get { return m_szName; }
set { m_szName = value; }
}
}

[Serializable]
public class Child2
{
private string m_szXml;

public Child1()
{
}

[XmlAttribute("Name")]
public String Xml
{
get { return m_szXml; }
set { m_szXml= value; }
}
}
[Serializable]
public class RootElement
{
private Child1 m_oChild1;
private Child2 m_oChild2;

public RootElement()
{
}

[XmlElement("Child1")]
public Child1 Child1
{
get { return m_oChild1; }
set { m_oChild1 = value; }
}

[XmlElement("Child2")]
public Child2 Child2
{
get { return m_oChild2; }
set { m_oChild2 = value; }
}
}


class Program
{
static void Main(string[] args)
{

System.IO.Stream oXmlStream = null;
oXmlStream =
System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("Test.Sample.xml");


System.Xml.Serialization.XmlSerializer oSerializer =
new
System.Xml.Serialization.XmlSerializer(typeof(RootElement));

RootElement oRootElement = oSerializer.Deserialize(oXmlStream )
as RootElement;

// String szInitialization = oRootElement.ChildRaw.Raw;
String szXml= oRootElement.Child2.Xml;

// ??? here I would like my szXml to
// ??? be "<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>"
}
}

Any ideas? Please help. I am trying to avoid to parse the XML by hand.

As a background... The reason I am looking to implement this is because in
the architecture I am developing, the main application may not know about the
classes (plug-in components) that could be serialized from the main XML
configuration file. I would like to be able to simply extract XML branches
from the main configuration and pass it on to the plug-in components that
would know what to do with it.

Thank you,
Vic

I think it may be a CDATA thing... not sure. But whenever I do serializing
like this i create the classes like you did, set their properties, then
serialize it into xml to see what the output would be so I know specifically
how to modify the xml to get a desired effect. So in your case create a test
or console project create an instance of the class, set the property to some
xml string you want to use and serialize it to see how it is saved off.
 
C

Chris Dunaway

Ian,

Thanks for your post. I think you are correct about CDATA. At the same time,
I am trying to de-serialize this specific XML. Which means, instead of making
changes to the XML, I'd like to change the code so that it would process the
XML the way I needed: to have a string member of a class return its inner XML.

Is that possible?

Thanks,
Vic

Ian McCaul said:
I am trying to create such class that could be serialized/deserialized
to/from an XML. I have chosen to use System.Xml.Serialization.XmlSerializer
serializer for that.
There is a caveat though. I would like to have one of my class's members to
be a string, which would contain an XML text. Here is what I mean.
My XML file:
<?xml version="1.0" encoding="utf-8" ?>
<RootElement>
<Child1 Name="1"/>
<Child2>
<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>
</Child2>
</RootElement>
I would like to be able to deserialize my RootElement and by refering to
Child2 be able to access the XML string
"<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>".
Here is what I have done so far:
[Serializable]
public class Child1
{
private string m_szName;
public Child1()
{
}
[XmlAttribute("Name")]
public String Name
{
get { return m_szName; }
set { m_szName = value; }
}
}
[Serializable]
public class Child2
{
private string m_szXml;
public Child1()
{
}
[XmlAttribute("Name")]
public String Xml
{
get { return m_szXml; }
set { m_szXml= value; }
}
}
[Serializable]
public class RootElement
{
private Child1 m_oChild1;
private Child2 m_oChild2;
public RootElement()
{
}
[XmlElement("Child1")]
public Child1 Child1
{
get { return m_oChild1; }
set { m_oChild1 = value; }
}
[XmlElement("Child2")]
public Child2 Child2
{
get { return m_oChild2; }
set { m_oChild2 = value; }
}
}
class Program
{
static void Main(string[] args)
{
System.IO.Stream oXmlStream = null;
oXmlStream =
System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("Test.Sample.xml");
System.Xml.Serialization.XmlSerializer oSerializer =
new
System.Xml.Serialization.XmlSerializer(typeof(RootElement));
RootElement oRootElement = oSerializer.Deserialize(oXmlStream )
as RootElement;
// String szInitialization = oRootElement.ChildRaw.Raw;
String szXml= oRootElement.Child2.Xml;
// ??? here I would like my szXml to
// ??? be "<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>"
}
}
Any ideas? Please help. I am trying to avoid to parse the XML by hand.
As a background... The reason I am looking to implement this is because in
the architecture I am developing, the main application may not know about the
classes (plug-in components) that could be serialized from the main XML
configuration file. I would like to be able to simply extract XML branches
from the main configuration and pass it on to the plug-in components that
would know what to do with it.
Thank you,
Vic
I think it may be a CDATA thing... not sure. But whenever I do serializing
like this i create the classes like you did, set their properties, then
serialize it into xml to see what the output would be so I know specifically
how to modify the xml to get a desired effect. So in your case create a test
or console project create an instance of the class, set the property to some
xml string you want to use and serialize it to see how it is saved off.

I don't think it's possible with the Xml you presented. The xml data
within the <SomeXml> tag would have to be properly escaped and look
something like this (like you, I typed this by hand so there may be
typos):

<?xml version="1.0" encoding="utf-8" ?>
<RootElement>
<Child1 Name="1"/>
<Child2>
<SomeXml>&lt;SomeMoreXml&gt;test&lt;/SomeMoreXml&gt;</SomeXml>
</Child2>
</RootElement>

Notice that the xml data in the SomeXml tags is escaped. As Peter
mentioned, if you use the Xml Serializer to create the XML, this will
happen automatically.

Chris
 
V

VRSki

Thank you.

Peter Duniho said:
[...] What I am
trying to do, though, is to write such code (using [Xml...] attributes
and
possibly overriding attributes) that could deserialize that specific XML
that
I provided the way I expect it. Which is to have a string member of a
class
return its inner XML.

Is that possible?

No, I don't think so. There is a clear division between data and the XML
elements itself. You cannot expect XML elements to be interpreted as data
unless you write your own deserialization code that treats that case
specially.

Pete
 
V

VRSki

Thanks Chris.

Unfortunately escaping is not an option... I was hoping there were some
[Attributes] to apply to a class to tell the Deserializer to just ignore
deserialization of the inner block... Oh well..

I guess I'll just have to do it by hand :)

Thanks again,
Vic

Chris Dunaway said:
Ian,

Thanks for your post. I think you are correct about CDATA. At the same time,
I am trying to de-serialize this specific XML. Which means, instead of making
changes to the XML, I'd like to change the code so that it would process the
XML the way I needed: to have a string member of a class return its inner XML.

Is that possible?

Thanks,
Vic

Ian McCaul said:
"(e-mail address removed)" wrote:
I am trying to create such class that could be serialized/deserialized
to/from an XML. I have chosen to use System.Xml.Serialization.XmlSerializer
serializer for that.
There is a caveat though. I would like to have one of my class's members to
be a string, which would contain an XML text. Here is what I mean.
My XML file:
<?xml version="1.0" encoding="utf-8" ?>
<RootElement>
<Child1 Name="1"/>
<Child2>
<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>
</Child2>
</RootElement>
I would like to be able to deserialize my RootElement and by refering to
Child2 be able to access the XML string
"<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>".
Here is what I have done so far:
[Serializable]
public class Child1
{
private string m_szName;
public Child1()
{
}
[XmlAttribute("Name")]
public String Name
{
get { return m_szName; }
set { m_szName = value; }
}
}
[Serializable]
public class Child2
{
private string m_szXml;
public Child1()
{
}
[XmlAttribute("Name")]
public String Xml
{
get { return m_szXml; }
set { m_szXml= value; }
}
}
[Serializable]
public class RootElement
{
private Child1 m_oChild1;
private Child2 m_oChild2;
public RootElement()
{
}
[XmlElement("Child1")]
public Child1 Child1
{
get { return m_oChild1; }
set { m_oChild1 = value; }
}
[XmlElement("Child2")]
public Child2 Child2
{
get { return m_oChild2; }
set { m_oChild2 = value; }
}
}
class Program
{
static void Main(string[] args)
{
System.IO.Stream oXmlStream = null;
oXmlStream =
System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("Test.Sample.xml");
System.Xml.Serialization.XmlSerializer oSerializer =
new
System.Xml.Serialization.XmlSerializer(typeof(RootElement));
RootElement oRootElement = oSerializer.Deserialize(oXmlStream )
as RootElement;
// String szInitialization = oRootElement.ChildRaw.Raw;
String szXml= oRootElement.Child2.Xml;
// ??? here I would like my szXml to
// ??? be "<SomeXml><SomeMoreXml>test</SomeXml></SomeMoreXml>"
}
}
Any ideas? Please help. I am trying to avoid to parse the XML by hand.
As a background... The reason I am looking to implement this is because in
the architecture I am developing, the main application may not know about the
classes (plug-in components) that could be serialized from the main XML
configuration file. I would like to be able to simply extract XML branches
from the main configuration and pass it on to the plug-in components that
would know what to do with it.
Thank you,
Vic
I think it may be a CDATA thing... not sure. But whenever I do serializing
like this i create the classes like you did, set their properties, then
serialize it into xml to see what the output would be so I know specifically
how to modify the xml to get a desired effect. So in your case create a test
or console project create an instance of the class, set the property to some
xml string you want to use and serialize it to see how it is saved off.

I don't think it's possible with the Xml you presented. The xml data
within the <SomeXml> tag would have to be properly escaped and look
something like this (like you, I typed this by hand so there may be
typos):

<?xml version="1.0" encoding="utf-8" ?>
<RootElement>
<Child1 Name="1"/>
<Child2>
<SomeXml><SomeMoreXml>test</SomeMoreXml></SomeXml>
</Child2>
</RootElement>

Notice that the xml data in the SomeXml tags is escaped. As Peter
mentioned, if you use the Xml Serializer to create the XML, this will
happen automatically.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top