How to save XmlDocument with multiple indentation settings?
Asked Answered
D

1

5

I need to save one XmlDocument to file with proper indentation (Formatting.Indented) but some nodes with their children have to be in one line (Formatting.None).

How to achieve that since XmlTextWriter accept setting for a whole document?


Edit after @Ahmad Mageed's resposne:

I didn't know that XmlTextWriter settings can be modified during writing. That's good news.

Right now I'm saving xmlDocument (which is already filled with nodes, to be specific it is .xaml page) this way:

XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
xmlDocument.WriteTo(writer);
writer.Flush();
writer.Close();

It enables indentation in all nodes, of course. I need to disable indentation when dealing with all <Run> nodes.

In your example you write to XmlTextWriter "manually". Is there an easy way to crawl through all xmlDocument nodes and write them to XmlTextWriter so I can detect <Run> nodes? Or do I have to write some kind of recursive method that will go to every child of current node?

Digiovanni answered 9/1, 2010 at 22:42 Comment(2)
@shg: I saw your reply. Is LINQ to XML an option? Or strictly .NET 2.0? BTW you should edit your question and post that code here instead of posting it as an answer so everything is in one spot for others to see since it's additional info for your question. You can edit this one then delete the other post.Angus
@Ahmad Mageed: I have never used LINQ to XML before but it is an option. Thanks for the hint. I will take a closer look to it. BTW I have made the edit you suggested. I am new to posting questions on stackoverflow so sorry for the mess.Digiovanni
A
3

What do you mean by "since XmlTextWriter accept setting for a whole document?" The XmlTextWriter's settings can be modified, unlike XmlWriter's once set. Similarly, how are you using XmlDocument? Please post some code to show what you've tried so that others have a better understanding of the issue.

If I understood correctly, you could modify the XmlTextWriter's formatting to affect the nodes you want to appear on one line. Once you're done you would reset the formatting back to be indented.

For example, something like this:

XmlTextWriter writer = new XmlTextWriter(...);
writer.Formatting = Formatting.Indented;
writer.Indentation = 1;
writer.IndentChar = '\t';

writer.WriteStartElement("root");

// people is some collection for the sake of an example
for (int index = 0; index < people.Count; index++)
{
    writer.WriteStartElement("Person");

    // some node condition to turn off formatting
    if (index == 1 || index == 3)
    {
        writer.Formatting = Formatting.None;
    }

    // write out the node and its elements etc.
    writer.WriteAttributeString("...", people[index].SomeProperty);
    writer.WriteElementString("FirstName", people[index].FirstName);

    writer.WriteEndElement();

    // reset formatting to indented
    writer.Formatting = Formatting.Indented;
}

writer.WriteEndElement();
writer.Flush();
Angus answered 10/1, 2010 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.