0

I have an xml file which is like so.

<Root>
  <Child123>
   more nodes inside 
   </Child123>
  <Child123></Child123>
  <Child123></Child123>
  <Child123></Child123>
</Root>

My code is generating this file which is not correctly formatting its forgetting to place a hard return at only the <Cild123>. You will see on line 2 the 3 line Child123 is starting on Line 2 when it should be on Line 3?

<Root>
  <Child123></Child123>
  <Child123></Child123><Child123>

  more nodes inside

  </Child123><Child123>
 more nodes inside
</Child123>
</Root>

This is my code I read the file into a list view and allow the user to pick some lines then I click generate, this then allows the above file to be generated

public static string Seralize<T>(T dataToSerlize)
{
   var ns = new XmlSerializerNamespaces();
   ns.Add("", "");
   var seralize = new XmlSerializer(dataToSerlize.GetType());
   var settings = new XmlWriterSettings();
   settings.Indent = true;
   settings.OmitXmlDeclaration = true;
   settings.NewLineChars = "\n";          
   settings.NewLineHandling = NewLineHandling.Replace;

   using (var stream = new StringWriter())
   {
       using (var test =  XmlWriter.Create(stream, settings))
       {
           seralize.Serialize(test, dataToSerlize, ns);
           return stream.ToString();
       }                
   }
} 

But as you see its not keeping the formatting correct in the generated xml file how to I ensure that it retains the flow of the first xml

PS I also tried

settings.Encoding = Encoding.UTF-8;

Which I thought may be the issue.

I also tried

settings.NewLineChars = "\n";          

But still no joy.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • You need to use and not \n, see https://stackoverflow.com/questions/2265966/xml-carriage-return-encoding – Arun Kumar Jul 02 '21 at 18:56
  • 1
    @ArunKumar: OP doesn't want to insert a newline in text. He wants to insert a newline before a tag – Thomas Weller Jul 02 '21 at 18:59
  • Does this answer your question? [Indentation and new line command for XMLwriter in C#](https://stackoverflow.com/questions/4094180/indentation-and-new-line-command-for-xmlwriter-in-c-sharp). Looks like you may want to use the `XmlTextWriter` instead of `XmlWriter`. – quaabaam Jul 02 '21 at 19:00
  • I think the issue is where `Seralize()` is called. The given code only serializes a single datum. How are many datums put together? – Thomas Weller Jul 02 '21 at 19:30
  • If you set `settings.Indent=true` then you're giving the serializer control over the whitespace formatting in the result. – Michael Kay Jul 02 '21 at 21:54
  • If i dont set ident then it all on one line the xml @MichaelKay – c-sharp-and-swiftui-devni Jul 04 '21 at 11:42

1 Answers1

0

One approach would be to focus your efforts on generating valid XML and let XDocument handle the formatting. Assuming your generated XML is valid, you can do this with it:

using System.Xml.Linq;

string rawXml = 
    @"<Root>
        <Child123></Child123>
        <Child123></Child123><Child123>
        </Child123><Child123>
        </Child123>
      </Root>";

XDocument formattedXml = XDocument.Parse(rawXml);

The output of formattedXml.ToString() is:

<Root>
  <Child123></Child123>
  <Child123></Child123>
  <Child123></Child123>
  <Child123></Child123>
</Root>
Tawab Wakil
  • 1,737
  • 18
  • 33