473,387 Members | 1,876 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,387 software developers and data experts.

Formatting XmlTextWriter Output

// I want this format...
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:media="http://search.yahoo.com/mrss/">

The WriteStartElement is used for the rss element and extended using the
WriteAttributeString method which outputs the version and namespace
attributes. Can someone tell me how to format the output as shown above?

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/

Apr 12 '07 #1
13 6232
clintonG wrote:
// I want this format...
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:media="http://search.yahoo.com/mrss/">

The WriteStartElement is used for the rss element and extended using the
WriteAttributeString method which outputs the version and namespace
attributes. Can someone tell me how to format the output as shown above?
With .NET 2.0 you can use
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.NewLineOnAttributes = true;
writerSettings.Indent = true;
then use those settings to create your writer e.g.
using (XmlWriter writer = XmlWriter.Create(@"file.xml", writerSettings))
{
// write XML here
}
That will not give exactly what you have specified above I think but at
least each attribute should be written on a line of its own.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 12 '07 #2
That's something to work with thanks...

<%= Clinton

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:ur*************@TK2MSFTNGP05.phx.gbl...
clintonG wrote:
>// I want this format...
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:media="http://search.yahoo.com/mrss/">

The WriteStartElement is used for the rss element and extended using the
WriteAttributeString method which outputs the version and namespace
attributes. Can someone tell me how to format the output as shown above?

With .NET 2.0 you can use
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.NewLineOnAttributes = true;
writerSettings.Indent = true;
then use those settings to create your writer e.g.
using (XmlWriter writer = XmlWriter.Create(@"file.xml", writerSettings))
{
// write XML here
}
That will not give exactly what you have specified above I think but at
least each attribute should be written on a line of its own.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Apr 12 '07 #3
Amazing!
The inclusion of the XmlWriterSettings object will not allow any string
arguments that contain a colon.

// this will now barf...
writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");

<%= Clinton

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:ur*************@TK2MSFTNGP05.phx.gbl...
clintonG wrote:
>// I want this format...
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:media="http://search.yahoo.com/mrss/">

The WriteStartElement is used for the rss element and extended using the
WriteAttributeString method which outputs the version and namespace
attributes. Can someone tell me how to format the output as shown above?

With .NET 2.0 you can use
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.NewLineOnAttributes = true;
writerSettings.Indent = true;
then use those settings to create your writer e.g.
using (XmlWriter writer = XmlWriter.Create(@"file.xml", writerSettings))
{
// write XML here
}
That will not give exactly what you have specified above I think but at
least each attribute should be written on a line of its own.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Apr 13 '07 #4
clintonG wrote:
// this will now barf...
writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
You need to use
write.WriteAttributeString(
"xmlns",
"df",
"http://www.w3.org/2000/xmlns/",
"http://purl.org/dc/elements/1.1/"
);
or
write.WriteAttributeString(
"xmlns",
"df",
null,
"http://purl.org/dc/elements/1.1/"
);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 13 '07 #5
I try to read documentation but I didn't observe how to use overloaded
methods in this context. The documentation certainly appears to be
consistent. Could you tell me what part of the documentation typifys methods
which can be overloaded? I mean what I need to learn to look for?

I'll try it out Martin and thanks fro bringing this to my attention...

<%= Clinton

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:uf**************@TK2MSFTNGP06.phx.gbl...
clintonG wrote:
>// this will now barf...
writer.WriteAttributeString("xmlns:dc",
"http://purl.org/dc/elements/1.1/");

You need to use
write.WriteAttributeString(
"xmlns",
"df",
"http://www.w3.org/2000/xmlns/",
"http://purl.org/dc/elements/1.1/"
);
or
write.WriteAttributeString(
"xmlns",
"df",
null,
"http://purl.org/dc/elements/1.1/"
);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Apr 13 '07 #6
No errors but...

// same single line persists
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">

And...I'm in some relevant documentation at the moment [1] but observe
undocumented parameters being used, i.e. null and your suggested alternative
use of "http://www.w3.org/2000/xmlns/" both of which I've tried but still
get a single line result.

// As documented [1] the method takes two and only two arguments
// XmlWriter.WriteAttributeString Method (String, String)
public void WriteAttributeString (
string localName,
string value
)

<%= Clinton

[1] http://msdn2.microsoft.com/en-us/lib...d8(vs.80).aspx

"clintonG" <no****@nowhere.comwrote in message
news:Ov**************@TK2MSFTNGP02.phx.gbl...
>I try to read documentation but I didn't observe how to use overloaded
methods in this context. The documentation certainly appears to be
consistent. Could you tell me what part of the documentation typifys
methods which can be overloaded? I mean what I need to learn to look for?

I'll try it out Martin and thanks fro bringing this to my attention...

<%= Clinton

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:uf**************@TK2MSFTNGP06.phx.gbl...
>clintonG wrote:
>>// this will now barf...
writer.WriteAttributeString("xmlns:dc",
"http://purl.org/dc/elements/1.1/");

You need to use
write.WriteAttributeString(
"xmlns",
"df",
"http://www.w3.org/2000/xmlns/",
"http://purl.org/dc/elements/1.1/"
);
or
write.WriteAttributeString(
"xmlns",
"df",
null,
"http://purl.org/dc/elements/1.1/"
);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Apr 13 '07 #7
Got the documentation and parameter list issue resolved. Still left with
trying to resolve why the XmlWriterSettings object properties are not being
applied.

<%= Clinton

"clintonG" <no****@nowhere.comwrote in message
news:O0*************@TK2MSFTNGP04.phx.gbl...
No errors but...

// same single line persists
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">

And...I'm in some relevant documentation at the moment [1] but observe
undocumented parameters being used, i.e. null and your suggested
alternative use of "http://www.w3.org/2000/xmlns/" both of which I've
tried but still get a single line result.

// As documented [1] the method takes two and only two arguments
// XmlWriter.WriteAttributeString Method (String, String)
public void WriteAttributeString (
string localName,
string value
)

<%= Clinton

[1] http://msdn2.microsoft.com/en-us/lib...d8(vs.80).aspx

"clintonG" <no****@nowhere.comwrote in message
news:Ov**************@TK2MSFTNGP02.phx.gbl...
>>I try to read documentation but I didn't observe how to use overloaded
methods in this context. The documentation certainly appears to be
consistent. Could you tell me what part of the documentation typifys
methods which can be overloaded? I mean what I need to learn to look for?

I'll try it out Martin and thanks fro bringing this to my attention...

<%= Clinton

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:uf**************@TK2MSFTNGP06.phx.gbl...
>>clintonG wrote:

// this will now barf...
writer.WriteAttributeString("xmlns:dc",
"http://purl.org/dc/elements/1.1/");

You need to use
write.WriteAttributeString(
"xmlns",
"df",
"http://www.w3.org/2000/xmlns/",
"http://purl.org/dc/elements/1.1/"
);
or
write.WriteAttributeString(
"xmlns",
"df",
null,
"http://purl.org/dc/elements/1.1/"
);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/



Apr 13 '07 #8
clintonG wrote:
No errors but...

// same single line persists
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
I am not sure why the xmlns declarations end up on the same line, that
seems a bug to me. Or the writers of the XmlWriter API treat xmlns
declaration attributes intentionally different although I can't see a
reason for that, if NewLineOnAttributes is true then that should apply
to xmlns attributes as well in my opinion.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 14 '07 #9

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:e7**************@TK2MSFTNGP03.phx.gbl...
clintonG wrote:
>No errors but...

// same single line persists
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">

I am not sure why the xmlns declarations end up on the same line, that
seems a bug to me. Or the writers of the XmlWriter API treat xmlns
declaration attributes intentionally different although I can't see a
reason for that, if NewLineOnAttributes is true then that should apply to
xmlns attributes as well in my opinion.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
I'm going to try to replicate with a new file sometime today. With all the
brooha about XML being a readable format one would think there would have
been more emphasis on formatting the raw XML.

It was also interesting to note Intellisense displays a Settings property
for an instance of an XmlWriter object but barfs when it is used to try to
set a property. I think (what I consider to be a redundant) instance of an
XmlWriterSettings object is going to be required and will force all
attributes in the entire file to be formatted whether wanted or not. If it
actually functions fot this purpose. Thanks for paying attention...

<%= Clinton
Apr 14 '07 #10
So far as I can tell, that is how settings are built and applied - just like you said - they apply to the whole document and I don't believe can be set on an element/attribute basis:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter xw = XmlWriter.Create(filename, settings);

What I'm running into is where I want to assign both an attribute and content to an element, i.e. to produce

<content encoding="base64binary">encoded content here</content>

it doesn't like it unless I do a "WriteRaw" for the content:

xw.WriteStartElement("content", null);
xw.WriteAttributeString("encoding", "base64binary");
xw.WriteRaw(messageContent);
xw.WriteEndElement(); // end the content element

This comes out fine, but I was just curious if there was another, better, way?

-Tom

Sep 19 '08 #11
It didn't like my XML in that post above. Here, again, is what I'm trying to produce:

"bracket" content encoding="base64encoding" "bracket" encoded content here....
"bracket" /content "bracket"

Sep 19 '08 #12
3rd time's a charm? Just change out the semi-colons below for brackets. I tried typing "bracket" before, above, and it didn't like it/wouldn't post it.

Expand|Select|Wrap|Line Numbers
  1.  
  2. ;content encoded="base64binary";my encoded content here;/content;
  3.  
  4.  
Sep 19 '08 #13
Tom Jackson wrote:
What I'm running into is where I want to assign both an attribute and content to an element, i.e. to produce

<content encoding="base64binary">encoded content here</content>

it doesn't like it unless I do a "WriteRaw" for the content:

xw.WriteStartElement("content", null);
xw.WriteAttributeString("encoding", "base64binary");
xw.WriteRaw(messageContent);
xw.WriteEndElement(); // end the content element

This comes out fine, but I was just curious if there was another, better, way?
What does "it doesn't like it" mean exactly? Which code exactly have you
tried? Which error exactly did you get?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Sep 19 '08 #14

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

Similar topics

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...
6
by: Ot | last post by:
I have an xml document similar to this <all> <one> <options attr1="1" attr2="2" /> <item name="a name" attr1="1" /> <item name="another" attr1="2"/> </one> <two> <options attr1="1"...
2
by: Miki Szinegh | last post by:
Hello All, I have an asp.net search application that brings up a datagrid of results, with one of the fields being a hyperlink to an XML document, of which there are thousands. By clicking this...
2
by: vector | last post by:
Here is a function more or less exactly as I found it from somewhere on the internet. static string BeautifyXML(string sXML) { string result = ""; System.IO.MemoryStream ms = new...
1
by: Riko Eksteen | last post by:
Hi I'm reading an xml file into an XmlDocument, adding some nodes, and writing it back out. I would like the nodes I add to assume the same level of indeting as the rest of the document. (I load...
4
by: Adrian Parker | last post by:
I have an xml document object with xml in it, how can I convert the xml to a string and format it so that it is is indented correctly. I've looked at XMLTextWriter and the Format property, but am...
2
by: Ken Wilson | last post by:
I am writing and .xml file and am not getting the formatting I would like. The portion of the code that is giving me problems is as follows; XmlTextWriter tw = new XmlTextWriter(filename); ...
3
by: Urs Vogel | last post by:
Hi When using the XmlDocument.OuterXml property after adding some nodes and attributes, it returns the entire document correctly, but it's actually all on one line. How can I format the document...
0
by: marfi95 | last post by:
I have an xml file that I want to maintain the whitespace on and spacing between my sections. I have something like this. <!-- Comment lines A --> <!-- Comment lines A --> <A> <B>test</B>...
1
by: JAM | last post by:
I'm trying to code some directory structure as my output / input file using XML. I would like to see formatiing with indentations. mimicking directory tree structure. Unfortunately the code such as...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.