473,796 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Manually building an XmlDocument

Hello,
Simple question this one.

How can I manually build a simple xmldocument in code.
Lets say for example

<Products>
<Boat id="1">
<Price>9000</Price>
</Boat>
</Products>

thank you very much.
Nov 17 '05 #1
3 7837
Hi,

You need to use System.XML namespace for that. Here is a small example of
creating an XML document through C#:
using System;
using System.Xml;

namespace Test
{
class MainClass
{
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;
XmlElement xmlelem2;
XmlText xmltext;
static void Main(string[] args)
{
MainClass app=new MainClass();
}
public MainClass() //constructor
{
xmldoc=new XmlDocument();
//let's add the XML declaration section
xmlnode=xmldoc. CreateNode(XmlN odeType.XmlDecl aration,"","");
xmldoc.AppendCh ild(xmlnode);
//let's add the root element
xmlelem=xmldoc. CreateElement(" ","ROOT","" );
xmltext=xmldoc. CreateTextNode( "This is the text of the root element");
xmlelem.AppendC hild(xmltext);
xmldoc.AppendCh ild(xmlelem);
//let's add another element (child of the root)
xmlelem2=xmldoc .CreateElement( "","SampleEleme nt","");
xmltext=xmldoc. CreateTextNode( "The text of the sample element");
xmlelem2.Append Child(xmltext);
xmldoc.ChildNod es.Item(1).Appe ndChild(xmlelem 2);
//let's try to save the XML document in a file: C:\pavel.xml
try
{
xmldoc.Save("c: \mydoc.xml");
}
catch (Exception e)
{
Console.WriteLi ne(e.Message);
}
Console.ReadLin e();
}
}
}

Hope this helps.

Thanks

Mona[Grapecity]
"CodeRazor" <Co*******@disc ussions.microso ft.com> wrote in message
news:03******** *************** ***********@mic rosoft.com...
Hello,
Simple question this one.

How can I manually build a simple xmldocument in code.
Lets say for example

<Products>
<Boat id="1">
<Price>9000</Price>
</Boat>
</Products>

thank you very much.

Nov 17 '05 #2
thank you Mona, thats what i was looking for.
Nov 17 '05 #3
CodeRazor wrote:
Hello,
Simple question this one.

How can I manually build a simple xmldocument in code.
Lets say for example

<Products>
<Boat id="1">
<Price>9000</Price>
</Boat>
</Products>

thank you very much.


if you are just interested in the xml *text* (at first), you can use this:

----------------------
using System.IO;
using System.Text;
using System.Xml;
StringBuilder sb = new StringBuilder() ;
StringWriter sw = new StringWriter(sb );
XmlTextWriter xw = new XmlTextWriter(s w);
xw.Formatting = Formatting.Inde nted; // optional

xw.WriteStartEl ement("Products ");
xw.WriteStartEl ement("Boat");
xw.WriteAttribu teString("id", "1");
xw.WriteElement String("Price", "9000");
xw.WriteEndElem ent();
xw.WriteEndElem ent();

Console.WriteLi ne(sb.ToString( )); // or any other way to "use" the xml-text
----------------------

Hans Kesting
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
17140
by: Martin Honnen | last post by:
With both .NET 1.0 and 1.1 I have found the following strange behaviour where System.Xml.XmlDocument.LoadXml doesn't throw an error when parsing a text node with a character reference to an invalid characters like &#x1;. Using the CreateTextNode method I create a text node containing "\u0001a" (C# string literal notation). As far as I understand the DOM allows that and an implementation is not required to throw an error. When OuterXml is...
2
6044
by: Graham Pengelly | last post by:
Hi I am trying to transform on System.Xml.XmlDocument into another using XslTransform without writing the object out to a file. I am guessing it should work something like this... public XmlDocument TransformXmlDoc(XmlDocument docToTransform, string xsltFilePath) { //load the xslt
2
6211
by: Dave | last post by:
Hi, Is there an easier way to pull a subset of nodes from one XmlDocument to another? I have the code below but would like to know if there is a more streamlined method. Thanks, Dave XmlNodeList nodeList = doc1.SelectNodes("//row"); foreach (XmlNode nodeCode in nodeList) { sb.Append(nodeCode.OuterXml);
8
6166
by: pete | last post by:
Hi there, Can someone explain to me why I can't bind to an XmlDocument but I can bind to an XmlNodeList. It's my understanding that they both implement the IEnumerable interface which is required for databinding. Am happy to work with the XmlNodeList but I can't shake the feeling I'm missing something <blush> Thanks, Pete
1
9699
by: Peter Nofelt | last post by:
Hey All, I'm running into this issue with parsing through an xml document by tag name. Below is an example xml document: File Name: things.xml <things> <people> <name>Peter</name>
5
10166
by: Rob Panosh | last post by:
Hello, I am trying to create a xmlDocument from as dataset. My code is listed below. All seems to go well until xmlDocument.Load(CType(ms, System.IO.Stream)) ... I keep getting the following error "The Root Element is Missing" ... any help would be appreciated. Thanks, Rob Panosh
3
1466
by: darrel | last post by:
I have three tables making a many-to-many relationship: Companies CoID | CoName Link CoID | InsID Insurers InsID | InsName
2
13597
by: John Smith | last post by:
I'm writing webervice client using .Net 2.0. I have this class: public class MyWebService : SoapHttpClientProtocol { public XmlDocument validate(string url, XmlDocument xmlDocument) { this.Url = url;
1
1180
Bob Ross
by: Bob Ross | last post by:
I am trying to build an XML document from a sataset using the XMLDocument. I need however to be able to find elements to add new elements based upon the attributes. For example - <website name="google"> <address>www.google.com</address> </website> <website name="yahoo"> <address>www.yahoo.com> </website>
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.