473,385 Members | 1,730 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.

XmlSerializer - Indent as tab instead of spaces

Hello,

does anyone know how to serialize an object to xml by using tabs as
indent instead of spaces.

My serializer code looks like following:

public static XmlDocument Serialize(object serializableObject)
{
XmlSerializer responseSerializer = new
XmlSerializer(serializableObject.GetType());

StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
try
{
ns.Add("", null);
ns.Add("xsd", "http://www.w3.org/2001/XMLSchema-instance");
responseSerializer.Serialize(writer, serializableObject, ns);
}
catch (Exception ex)
{
if (ex is InvalidOperationException)
throw ex.InnerException;
else
throw ex;
}
XmlDocument xmlResponse = new XmlDocument();
xmlResponse.LoadXml(sb.ToString());
return xmlResponse;
}

Thank you,
Norbert
Jun 27 '08 #1
6 15538
You need to write to an XmlWriter (which wraps, for example, your
StringWriter, StreamWriter, or whatever) which has been created with
suitable XmlWriterSettings - as below.

Marc

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
[Serializable]
public class Foo
{
public string Name { get; set; }
public int ShoeSize { get; set; }
}
static class Program
{
static void Main() {
Foo foo = new Foo { Name = "Fred", ShoeSize = 12 };

XmlWriterSettings settings = new XmlWriterSettings();
settings.IndentChars = "\t";
settings.Indent = true;

using(StringWriter sw = new StringWriter())
using (XmlWriter xw = XmlWriter.Create(sw, settings))
{
new XmlSerializer(typeof(Foo)).Serialize(xw, foo);
string xml = sw.ToString();
Console.WriteLine(xml);
}

}
}
Jun 27 '08 #2
If you can alter things to use an XMLTextWriter that has an IndentChar field
:

System.IO.StringWriter sw = new System.IO.StringWriter();

XmlTextWriter xmltw = new XmlTextWriter(sw);

xmltw.Formatting = Formatting.Indented;

xmltw.IndentChar = '\x09';
HTH,

Adam.
=======
Jun 27 '08 #3
On Jun 18, 11:08*am, "Adam Benson"
<Adam.Ben...@NOSPAMMYSPAM.omnibus.co.ukwrote:
If you can alter things to use an XMLTextWriter that has an IndentChar field

System.IO.StringWriter sw = new System.IO.StringWriter();

XmlTextWriter xmltw = new XmlTextWriter(sw);
xmltw.Formatting = Formatting.Indented;
xmltw.IndentChar = '\x09';
While you can do it this way, I prefer Marc's way of using
XmlWriterSettings. I also like to combine it with object initializers
in C# 3, which lets you do it all in one go if you want:

using (XmlWriter xw = XmlWriter.Create(sw,
new XmlWriterSettings { IndentChars="\t", Indent=true }))
{
...
}

Jon
Jun 27 '08 #4
Thank you for your advise using the XmlWriterSettings.

But the set Indent get lost, if I say something like that:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(stringWriter.ToString());
xmlDoc.Save(file);

Any idea how to say the XmlDocument to preserve the set indent?

Thank you,
Norbert
Jun 27 '08 #5
On Jun 18, 12:28*pm, Norbert Pürringer <thalio...@graffiti.netwrote:
Thank you for your advise using the XmlWriterSettings.

But the set Indent get lost, if I say something like that:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(stringWriter.ToString());
xmlDoc.Save(file);

Any idea how to say the XmlDocument to preserve the set indent?
It can't - the document itself has no concept of indentation. It's how
it's written that decides it. You'll need to save using a writer with
the appropriate settings.

Jon
Jun 27 '08 #6
Norbert Pürringer wrote:
But the set Indent get lost, if I say something like that:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(stringWriter.ToString());
xmlDoc.Save(file);

Any idea how to say the XmlDocument to preserve the set indent?
Have you tried to set
xmlDoc.PreserveWhitespace = true;
before you load?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #7

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

Similar topics

11
by: John Wellesz | last post by:
If you are coding php using GVIM, you will appreciate this new indent script: Download there: http://www.vim.org/scripts/download_script.php?src_id=3710 or here: ...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
21
by: Mark Hahn | last post by:
We're proud to say that Prothon has grown out of the indent space/tab flame war stage and has moved on to more serious discussions. We've decided to allow space-based indents and tab-based...
1
by: SteveB | last post by:
I'm porting an application from Apache Xerces to .Net and am having a couple of small problems with deserialization. The XML that I'm reading comes from a variety of sources, and there are two...
1
by: Rocky Zhou | last post by:
I am accustomed to vi my pthon scripts with 'tab' indent. But when I copy some code to my script, the indent may be 'spaces'. So I wanna a way to substitute those 'spaces' to be 'tabs'...
21
by: Bogdan | last post by:
Can anyone recommend a program for indentation of C preprocessor directives. My file looks like this: #ifdef a #define b #else #define c #endif int main() {
6
by: desktop | last post by:
I have this class: class case(blop.case): def __init__(self, n, a, b): blop.case.__init__(self) print 'Monty Python's Flying Circus has a ' within it...' ... ... But I get an error when I...
2
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; I would like to be able to use the XMLSerializer to serialize and deserialize a dictionary. is that possible? i know that you can serialize an object that implements the ICollection interface....
8
by: Duggi | last post by:
Hi In the following code why it is required to type cast while de- serialize... any ideas??? to create an object of XmlSerializer, Type is a compalsary parameter. Atleast by default...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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.