473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 4674
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anita C | last post by:
I have the foll. code to update the value of an attribute: xmlDocument.Load("abc.xml"); XmlAttribute xmlAttrib = xmlDocument.SelectSingleNode(root/web/theme/@desc); xmlAttrib.Value =...
0
by: Jocelyn Kwok | last post by:
Hi all, When I try to use XmlSerializer to serialize an ArrayList object which contains XmlNode objects, I got the "InvalidOperationException: The type System.Xml.XmlElement may not be used in...
2
by: David Elliott | last post by:
I am creating a configuration class to read / write a standard configuration file. <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="ConnectionString"...
3
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
1
by: Svyatoslav | last post by:
Hi, I have a problem with XmlNodes and my stack. It looks something like this: //declarations XmlNode node, new_node; Stack MyStack = new Stack(); //code MyStack.Push(node);
5
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
3
by: Mae | last post by:
Dear All, I have a problem here, I'm using C# Webform calling a webservices. The webservices return me a XMLnode, using this XMLnode I want to convert it to dataset so I can bind to the...
5
by: =?Utf-8?B?VGhlIE1hbiBGcm9tIFNRTA==?= | last post by:
I'm having the darndest XML config file problem that I really need help with. I'm supporting a .NET 1.1 desktop application with its own config file, and I implement IConfigurationSectionHandler...
2
by: =?iso-8859-1?Q?Norbert_P=FCrringer?= | last post by:
Hello! Is it possible to use the object XMLNode as a parameter in an interface function of a WCF service? In my case I get the error message: XmlNode ProcessServiceRequest(XmlNode request);...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.