471,082 Members | 837 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,082 software developers and data experts.

xmlnode as custom object

Greetings,

I have seen this question posted a few times, but the
answers have not helped me.

I have used the program xsd.exe to generate several c#
classes from a client provided xsd template. I now wish
take a sizeable amount of data and create an xml file
based on these classes. My intent is to use xmldocument
to build up the xml and then stream it to a file. My
problem is I cannot use my new classes as nodes since
they are not derived from nodes and they cannot be cast
as such.

What would be the best way to approach this?
Cheers, Ian Williamson
Nov 11 '05 #1
5 4558
Use the classes you created, then do the following:

YourClass c = new ...
// populate it...
XmlSerializer s = new XmlSerializer(typeof(YourClass));
s.Serialize(c, Console.Out);
"Ian Williamson" <bm******@nospam.shaw.ca> wrote in message
news:04****************************@phx.gbl...
Greetings,

I have seen this question posted a few times, but the
answers have not helped me.

I have used the program xsd.exe to generate several c#
classes from a client provided xsd template. I now wish
take a sizeable amount of data and create an xml file
based on these classes. My intent is to use xmldocument
to build up the xml and then stream it to a file. My
problem is I cannot use my new classes as nodes since
they are not derived from nodes and they cannot be cast
as such.

What would be the best way to approach this?
Cheers, Ian Williamson

Nov 11 '05 #2
I am at that point now. What I need to know is how to
take the serialized object and add it as a fully fleshed
out node.

If I do it like the code below, the serialized object
just ends up being a big string with all my properties
and attributes butted up beside each (ie. not formatted
XML). I know this is because I am defining the new node
as type Element, but I do not see any way to create a
complex node use CreateNode.

XmlDocument doc = new XmlDocument();
doc.LoadXml("<test></test>");
XmlNode root = (XmlNode)doc.DocumentElement;
XMLExport.Schemas.Transmission tran = new
XMLExport.Schemas.Transmission();
tran.BatchCount = "10";
StringWriter sw = new StringWriter(new StringBuilder());
XmlSerializer ser = new XmlSerializer(typeof
(XMLExport.Schemas.Transmission));
ser.Serialize(sw, tran);
XmlNode elem = doc.CreateNode
(XmlNodeType.Element, "transmission", sw.ToString());
root.AppendChild(elem);

[System.Serializable]
public class HBSTransmission {

[System.Xml.Serialization.XmlElementAttribute
(DataType="integer")]
public string BatchCount;
}
-----Original Message-----
Use the classes you created, then do the following:

YourClass c = new ...
// populate it...
XmlSerializer s = new XmlSerializer(typeof (YourClass)); s.Serialize(c, Console.Out);
"Ian Williamson" <bm******@nospam.shaw.ca> wrote in messagenews:04****************************@phx.gbl...
Greetings,

I have seen this question posted a few times, but the
answers have not helped me.

I have used the program xsd.exe to generate several c#
classes from a client provided xsd template. I now wish take a sizeable amount of data and create an xml file
based on these classes. My intent is to use xmldocument to build up the xml and then stream it to a file. My
problem is I cannot use my new classes as nodes since
they are not derived from nodes and they cannot be cast
as such.

What would be the best way to approach this?
Cheers, Ian Williamson

.

Nov 11 '05 #3
Ian,
the key insight here is that using xsd.exe to generate classes, you will be
doing XML Serialization. This means you will not (typically) be dealing
with XmlDocument, XmlNode, and XmlTextWriter, in your app code, but instead
with domain types (eg, YourClass, which is derived from your XSD). The XML
representing the content of the instances of those types, is generated for
you by the XmlSerializer.

If you want to go in the reverse direction - starting with XML data and
generating instances of classes from that XMl - you can do that also. Again
you use the serializer for this, not XmlDocument or XmlTextReader, etc.

"Chris Lovett" <chris@!nospam!.net> wrote in message
news:vi************@corp.supernews.com...
Use the classes you created, then do the following:

YourClass c = new ...
// populate it...
XmlSerializer s = new XmlSerializer(typeof(YourClass));
s.Serialize(c, Console.Out);
"Ian Williamson" <bm******@nospam.shaw.ca> wrote in message
news:04****************************@phx.gbl...
Greetings,

I have seen this question posted a few times, but the
answers have not helped me.

I have used the program xsd.exe to generate several c#
classes from a client provided xsd template. I now wish
take a sizeable amount of data and create an xml file
based on these classes. My intent is to use xmldocument
to build up the xml and then stream it to a file. My
problem is I cannot use my new classes as nodes since
they are not derived from nodes and they cannot be cast
as such.

What would be the best way to approach this?
Cheers, Ian Williamson


Nov 11 '05 #4
Looks like my original response got lost in the ether.

I am currently doing what you suggest, I just have no
idea how to take that serialized string, create a complex
node from it, and append it to my document.

XmlDocument doc = new XmlDocument();
XmlDeclaration decl = doc.CreateXmlDeclaration
("1.0", "UTF-8", null);
doc.AppendChild(decl);

Root myRoot = new Root();
myRoot.BatchCount = "10";
StringWriter sw = new StringWriter(new StringBuilder());
XmlSerializer ser = new XmlSerializer(typeof(Root));
ser.Serialize(sw, myRoot);
XmlNode elem = doc.CreateNode
(XmlNodeType.Element, "RootNode", "myURI");
elem.InnerText = sw.ToString();
doc.AppendChild(elem);

This of course just adds an unformatted string instead of
the complex node structure.

[System.Xml.Serialization.XmlRootAttribute
(Namespace="myURI", IsNullable=false)]
public class Root
{
[System.Xml.Serialization.XmlElementAttribute
(DataType="integer")]
public string BatchCount;

public string Type;

public string Creator;
}
-----Original Message-----
Use the classes you created, then do the following:

YourClass c = new ...
// populate it...
XmlSerializer s = new XmlSerializer(typeof (YourClass)); s.Serialize(c, Console.Out);
"Ian Williamson" <bm******@nospam.shaw.ca> wrote in messagenews:04****************************@phx.gbl...
Greetings,

I have seen this question posted a few times, but the
answers have not helped me.

I have used the program xsd.exe to generate several c#
classes from a client provided xsd template. I now wish take a sizeable amount of data and create an xml file
based on these classes. My intent is to use xmldocument to build up the xml and then stream it to a file. My
problem is I cannot use my new classes as nodes since
they are not derived from nodes and they cannot be cast
as such.

What would be the best way to approach this?
Cheers, Ian Williamson

.

Nov 11 '05 #5
Try using elem.InnerXml = sw.ToString(). InnerText escapes chars such as <
and >. InnerXml doesn't do this though.

HTH,
Dan Wahlin

Wahlin Consulting
Microsoft MVP- ASP.NET and XML Web Services
http://www.xmlforasp.net
"Ian Williamson" <bm******@nospam.shaw.ca> wrote in message
news:00****************************@phx.gbl...
Looks like my original response got lost in the ether.

I am currently doing what you suggest, I just have no
idea how to take that serialized string, create a complex
node from it, and append it to my document.

XmlDocument doc = new XmlDocument();
XmlDeclaration decl = doc.CreateXmlDeclaration
("1.0", "UTF-8", null);
doc.AppendChild(decl);

Root myRoot = new Root();
myRoot.BatchCount = "10";
StringWriter sw = new StringWriter(new StringBuilder());
XmlSerializer ser = new XmlSerializer(typeof(Root));
ser.Serialize(sw, myRoot);
XmlNode elem = doc.CreateNode
(XmlNodeType.Element, "RootNode", "myURI");
elem.InnerText = sw.ToString();
doc.AppendChild(elem);

This of course just adds an unformatted string instead of
the complex node structure.

[System.Xml.Serialization.XmlRootAttribute
(Namespace="myURI", IsNullable=false)]
public class Root
{
[System.Xml.Serialization.XmlElementAttribute
(DataType="integer")]
public string BatchCount;

public string Type;

public string Creator;
}
-----Original Message-----
Use the classes you created, then do the following:

YourClass c = new ...
// populate it...
XmlSerializer s = new XmlSerializer(typeof

(YourClass));
s.Serialize(c, Console.Out);
"Ian Williamson" <bm******@nospam.shaw.ca> wrote in

message
news:04****************************@phx.gbl...
Greetings,

I have seen this question posted a few times, but the
answers have not helped me.

I have used the program xsd.exe to generate several c#
classes from a client provided xsd template. I now wish take a sizeable amount of data and create an xml file
based on these classes. My intent is to use xmldocument to build up the xml and then stream it to a file. My
problem is I cannot use my new classes as nodes since
they are not derived from nodes and they cannot be cast
as such.

What would be the best way to approach this?
Cheers, Ian Williamson

.

Nov 11 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jocelyn Kwok | last post: by
2 posts views Thread by David Elliott | last post: by
3 posts views Thread by Mahesh Devjibhai Dhola | last post: by
5 posts views Thread by Mahesh Devjibhai Dhola | last post: by
5 posts views Thread by =?Utf-8?B?VGhlIE1hbiBGcm9tIFNRTA==?= | last post: by
2 posts views Thread by =?iso-8859-1?Q?Norbert_P=FCrringer?= | last post: by

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.