473,320 Members | 2,054 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,320 software developers and data experts.

Create xml with formatting

cnu
Hi,
I have to make a formatted (indented) xml file like this :

<?xml version="1.0"?>
<Message>
<Parent index="0" length="6">123
<Child1 index="0,1" length="6">abcd</Child1>
<Child2 index="0,2" length="6">pqrsxyz</Child2>
</Parent>
</Message>

As you can see, <Parent> node has some innertext as well as child nodes.
But, if
I make xml like this the indentation is lost. The resultant xml is like
this :

<?xml version="1.0"?>
<Message>
<Parent index="0" length="6">123<Child1 index="0,1"
length="6">99</Child1><Child2 index="0,2" length="6">pqrsxyz</Child2></Parent>
</Message>

To sum up the problem, if a node has both innertext and some child nodes,
then the entire node is written in a single line, without indentation.
How can I get the indentation in the above case??
Any help is highly appreciated.

Thanks.
Aug 25 '05 #1
5 2131
Is that even valid XML?
"cnu" <cn*@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
Hi,
I have to make a formatted (indented) xml file like this :

<?xml version="1.0"?>
<Message>
<Parent index="0" length="6">123
<Child1 index="0,1" length="6">abcd</Child1>
<Child2 index="0,2" length="6">pqrsxyz</Child2>
</Parent>
</Message>

As you can see, <Parent> node has some innertext as well as child nodes.
But, if
I make xml like this the indentation is lost. The resultant xml is like
this :

<?xml version="1.0"?>
<Message>
<Parent index="0" length="6">123<Child1 index="0,1"
length="6">99</Child1><Child2 index="0,2"
length="6">pqrsxyz</Child2></Parent>
</Message>

To sum up the problem, if a node has both innertext and some child nodes,
then the entire node is written in a single line, without indentation.
How can I get the indentation in the above case??
Any help is highly appreciated.

Thanks.

Aug 25 '05 #2
cnu
Though the format looks weird, it is a valid xml.
Unfortunately, I can't change the format...

Aug 25 '05 #3
On Wed, 24 Aug 2005 19:26:02 -0700, cnu wrote:

Hi!
To sum up the problem, if a node has both innertext and some child nodes,
then the entire node is written in a single line, without indentation.
How can I get the indentation in the above case??


You forgot to tell how you create the XML, I _assume_ you use
System.Xml.XmlWriter. In such case try it's 'Formating' property:

xmlWriter.Formatting = System.Xml.Formatting.Indented;

Cheers,
Piotrek

Aug 25 '05 #4
cnu
I have all the xml in XmlDocument and this is how I save :

System.Xml.XmlTextWriter xtw = new XmlTextWriter(aFilePath, null);
xtw.Formatting = Formatting.Indented;
xmlDoc.Save(xtw);
xtw.Flush();
xtw.Close();
Aug 25 '05 #5
On Thu, 25 Aug 2005 00:41:02 -0700, cnu wrote:

Hi!
System.Xml.XmlTextWriter xtw = new XmlTextWriter(aFilePath, null);
xtw.Formatting = Formatting.Indented;
xmlDoc.Save(xtw);
xtw.Flush();
xtw.Close();


It looks like you have found a bug in .NET XmlWriter :/
The code below seems to proove it... Any MVP here?

// --- the code ---
System.Xml.XmlTextWriter xtw = new XmlTextWriter(@"c:\temp\aTest.xml", System.Text.Encoding.UTF8);
xtw.Formatting = Formatting.Indented;

xtw.WriteStartElement("Message");
xtw.WriteStartElement("Parent");
xtw.WriteAttributeString("Index", "0");
xtw.WriteAttributeString("Length", "6");
xtw.WriteString("123"); // get rid of this line and everything is ok

xtw.WriteStartElement("Child1");
xtw.WriteAttributeString("index", "0,1");
xtw.WriteAttributeString("length", "6");
xtw.WriteString("abcd");

xtw.WriteEndElement();
xtw.WriteEndElement();
xtw.WriteEndElement();

xtw.Flush();
xtw.Close();
// --- end ---

Cheers,
Piotrek

Aug 25 '05 #6

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

Similar topics

3
by: Richard Fritzler | last post by:
I was given the task of designing a complete web based document prep system. In simplest terms (using a msword explanation) create a database of merge fields, and a library of templates. Allow the...
2
by: Terry | last post by:
I have cerated a Form, ExamsFrm, which is used to input details of exams taken at several Centres. It dosplays details from StudentTbl and has a SubForm which displays details from ExamsTble. I...
1
by: Mike P | last post by:
I am trying to create a new file and write to it, but I keep getting the error 'cannot access the file because it is being used by another process'. Can anybody help me out? long lngNextNumber...
2
by: ALI-R | last post by:
Hi All, I have an xml file with the structure below.I'd like to create dynamic number of xml files from this file based on <subreport> elements.each XML file contains subreport element only...
5
by: cnu | last post by:
Hi, I have to make a formatted (indented) xml file like this : <?xml version="1.0"?> <Message> <Parent index="0" length="6">123 <Child1 index="0,1" length="6">abcd</Child1> <Child2...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
4
by: pankajit09 | last post by:
Hello, How to create the formatting area which you see while composing an email ? The formatting area consist of the Bold, Italics, Underline etc.
0
by: remya1000 | last post by:
i'm using VB.NET. i'm using this codes to create this XML. TextBox1.Text = "C:\Program\Emp.mdb" If File.Exists(TextBox1.Text) Then Dim strSQL As String = "Select...
2
by: DragonLord | last post by:
Ok basically this is mostly working, What i want to do is create an xml file with multiple updates that have the same attribute. <root> <update> <element1>value</element1> ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.