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