473,795 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlDocument - Write problems & How to Add Indentation!

Hi there,

I have 2 questions to ask.

1) I have this code...

XmlDocument doc = new XmlDocument();
doc.Load(myXmlP ath);

int maxValue = 0;
int newValue = 0;

XmlNodeList nodeList = doc.SelectNodes ("//Product");

foreach (XmlNode node in nodeList)
maxValue = XmlConvert.ToIn t32(node.Attrib utes["id"].Value);

XmlElement newElem = doc.CreateEleme nt("Product");
newValue = maxValue + 1;
XmlAttribute newAttr = doc.CreateAttri bute("id");
newAttr.Value = newValue.ToStri ng();
newElem.Attribu tes.Append(newA ttr);

doc.DocumentEle ment.AppendChil d(newElem);
XmlElement elem = doc.CreateEleme nt("Description ");
elem.InnerText = "JVC Camera";
doc.DocumentEle ment.AppendChil d(elem);

XmlTextWriter wrtr = new XmlTextWriter(m yXmlPath, Encoding.UTF8);
doc.WriteTo(wrt r);
wrtr.Close();

Xml (Before) // xml has identation nicely
----------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nasonic Camcorder</Description>
</Product>
</ProductList>

Xml(After) // xml has no identation
--------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nasonic Camcorder</Description>
</Product>
<Product id="2" />
<Description>JV C Camera</Description>
</ProductList>

Xml (Expected this)
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nosonic Camcorder</Description>
</Product>
<Product id="2">
<Description>JV C Camera</Description>
</Product>
</ProductList>

Note: I want the Second Description tag to be part of Product id="2", but
with my C# codes, i can't achieve it.

2)

After i perform the C# code on top, my codes has no more identation. It
seems that it removes all the whitespaces to this. (Please take note, the
above xml examples, i manually re-arrange the generated xml for clear view).

Xml (After)
--------------
<?xml version="1.0" encoding="utf-8"?><ProductLis t><Product
id="1"><Descrip tion>Panasonic Camcorder</Description></Product><Produc t
id="2" /><Description>J VC Camera</Description></ProductList>

I hope i can indent nicely, just like what i did above on original xml file.

Any help please?
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com
Nov 16 '05 #1
2 5415
Hi,
1)
You created two element with same level, so you got that result.
The line "doc.DocumentEl ement.AppendChi ld(elem); " should be
"newElem.Append Child(elem);"

2)
You could apply to Formatting.Inde nted the Formatting property of
XmlTextWriter.

Alan

"Chua Wen Ching" <ch************ @nospam.hotmail .com> 写入消息新闻
:C2************ *************** *******@microso ft.com...
Hi there,

I have 2 questions to ask.

1) I have this code...

XmlDocument doc = new XmlDocument();
doc.Load(myXmlP ath);

int maxValue = 0;
int newValue = 0;

XmlNodeList nodeList = doc.SelectNodes ("//Product");

foreach (XmlNode node in nodeList)
maxValue = XmlConvert.ToIn t32(node.Attrib utes["id"].Value);

XmlElement newElem = doc.CreateEleme nt("Product");
newValue = maxValue + 1;
XmlAttribute newAttr = doc.CreateAttri bute("id");
newAttr.Value = newValue.ToStri ng();
newElem.Attribu tes.Append(newA ttr);

doc.DocumentEle ment.AppendChil d(newElem);
XmlElement elem = doc.CreateEleme nt("Description ");
elem.InnerText = "JVC Camera";
doc.DocumentEle ment.AppendChil d(elem);

XmlTextWriter wrtr = new XmlTextWriter(m yXmlPath, Encoding.UTF8);
doc.WriteTo(wrt r);
wrtr.Close();

Xml (Before) // xml has identation nicely
----------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nasonic Camcorder</Description>
</Product>
</ProductList>

Xml(After) // xml has no identation
--------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nasonic Camcorder</Description>
</Product>
<Product id="2" />
<Description>JV C Camera</Description>
</ProductList>

Xml (Expected this)
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nosonic Camcorder</Description>
</Product>
<Product id="2">
<Description>JV C Camera</Description>
</Product>
</ProductList>

Note: I want the Second Description tag to be part of Product id="2", but
with my C# codes, i can't achieve it.

2)

After i perform the C# code on top, my codes has no more identation. It
seems that it removes all the whitespaces to this. (Please take note, the
above xml examples, i manually re-arrange the generated xml for clear view).
Xml (After)
--------------
<?xml version="1.0" encoding="utf-8"?><ProductLis t><Product
id="1"><Descrip tion>Panasonic Camcorder</Description></Product><Produc t
id="2" /><Description>J VC Camera</Description></ProductList>

I hope i can indent nicely, just like what i did above on original xml file.
Any help please?
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com

Nov 16 '05 #2
Thanks Alan.

It works.

Cheers.

"Alan" wrote:
Hi,
1)
You created two element with same level, so you got that result.
The line "doc.DocumentEl ement.AppendChi ld(elem); " should be
"newElem.Append Child(elem);"

2)
You could apply to Formatting.Inde nted the Formatting property of
XmlTextWriter.

Alan

"Chua Wen Ching" <ch************ @nospam.hotmail .com> 脨麓脠毛脧没脧 ⒚惷偯幟
:C2************ *************** *******@microso ft.com...
Hi there,

I have 2 questions to ask.

1) I have this code...

XmlDocument doc = new XmlDocument();
doc.Load(myXmlP ath);

int maxValue = 0;
int newValue = 0;

XmlNodeList nodeList = doc.SelectNodes ("//Product");

foreach (XmlNode node in nodeList)
maxValue = XmlConvert.ToIn t32(node.Attrib utes["id"].Value);

XmlElement newElem = doc.CreateEleme nt("Product");
newValue = maxValue + 1;
XmlAttribute newAttr = doc.CreateAttri bute("id");
newAttr.Value = newValue.ToStri ng();
newElem.Attribu tes.Append(newA ttr);

doc.DocumentEle ment.AppendChil d(newElem);
XmlElement elem = doc.CreateEleme nt("Description ");
elem.InnerText = "JVC Camera";
doc.DocumentEle ment.AppendChil d(elem);

XmlTextWriter wrtr = new XmlTextWriter(m yXmlPath, Encoding.UTF8);
doc.WriteTo(wrt r);
wrtr.Close();

Xml (Before) // xml has identation nicely
----------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nasonic Camcorder</Description>
</Product>
</ProductList>

Xml(After) // xml has no identation
--------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nasonic Camcorder</Description>
</Product>
<Product id="2" />
<Description>JV C Camera</Description>
</ProductList>

Xml (Expected this)
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nosonic Camcorder</Description>
</Product>
<Product id="2">
<Description>JV C Camera</Description>
</Product>
</ProductList>

Note: I want the Second Description tag to be part of Product id="2", but
with my C# codes, i can't achieve it.

2)

After i perform the C# code on top, my codes has no more identation. It
seems that it removes all the whitespaces to this. (Please take note, the
above xml examples, i manually re-arrange the generated xml for clear

view).

Xml (After)
--------------
<?xml version="1.0" encoding="utf-8"?><ProductLis t><Product
id="1"><Descrip tion>Panasonic Camcorder</Description></Product><Produc t
id="2" /><Description>J VC Camera</Description></ProductList>

I hope i can indent nicely, just like what i did above on original xml

file.

Any help please?
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com


Nov 16 '05 #3

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

Similar topics

0
1660
by: James Thurley | last post by:
I'm creating an XmlDocument manually, adding content using the Xml classes such as XmlElement and XmlText, and I then write it out as as "text/xml" to the HttpResponse.Output TextWriter object under IIS. The problem I get is when I create an XmlElement with an XmlAttribute whose value contains the "&" character. When the xml is written the "&" has become "&amp;amp". This happens when I use XmlDocument.InnerXML or...
2
8693
by: Eric Zechman via .NET 247 | last post by:
I have a project in which I get xml posted via a hidden form field (data=<xml></xml>). People are entering names like Bill & Sandy Smith. this is coming over as: <NameInfo><FamilyName><FamilyNames>David &amp; Karen</FamilyNames><Surname>Adams</Surname></FamilyName></NameInfo> When I try to load this up in a XmlDocument it blows up. string xmlData = Request.Form; dom.LoadXml(xmlData); results with this: "This is an unexpected token. The...
3
5462
by: TS | last post by:
HI, is there any option while using appendChild so that it doesn't do the autotomatic indentation and line breaks so that the tags are appended back to back? thanks
1
3031
by: AJ Brown | last post by:
I'm loading an XmlDocument object from a string using LoadXml. The string is a well-formed XML fragment (see example) and loads fine, however there are entities (&amp;) within attribute values. The loader parses the incoming entities and converts them to their character references automatically. How would I retain the entity? Example incoming fragment: <company name="B &amp; B Electric" />
4
14808
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following expression: .... snip ...
7
4185
by: Mark | last post by:
Hi... A colleague just referred this question to me. He's getting an xml file from another party, which he's trying to process into another dom using an XmlTextReader and XmlDocument.ReadNode(). The problem is that it's breaking and he doesn't understand why. I didn't exactly either, which is why I'm posting a question here. First, his program just creates a new dom using new document like this: XmlDocument xml = new XmlDocument();
4
4183
by: David Grogan | last post by:
Hi, 2 questions.... 1. I'm parsing an XHTML document that contains both the default namespace (xmlns="http://www.w3.org/1999/xhtml") and a custom one (xmlns:r="...") - both of these being attributes of <html>. When I then insert new XmlNodes into the DOM the HTML tags all end up getting an unnecesarry 'xmlns' attribute added, elements with the 'r' prefix don't, which is correct. Is there any way to stop the XmlNode/XmlWriter from...
2
1603
by: Mike | last post by:
Hi, When using the following code: doc.PreserveWhitespace = true; doc.Save(fileName); where doc is a variable of type "XmlDocument", the XML file is saved with the correct information by the indentation is not kept. Any idea why? Also, the first character of the XML file is "锘". Any idea why?
5
3450
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot; &lt;me@mydomain.com&gt;</Address> </Addresses>
0
9522
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
10217
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...
0
10003
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
9046
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project梡lanning, coding, testing, and deployment梬ithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
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...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.