#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Formatting with xmlwriter


    I am using a little function (below) to format some xml before writing it out to file. It works pretty good, but there is one thing I'd like to change if possible. When it encounters a node without a value, it breaks it out to two lines. I want it to stay on one line. For example, it will format a node as such:
    <emptytag>
    </emptytag>
    I want it to be as such:
    <emptytag></emptytag>
    Is it possible to get xmlwriter to do this?

    Code:
    static public void Beautify(XmlDocument doc) 
    { 
    try
    {
    StringBuilder sb = new StringBuilder(); 
    XmlWriterSettings settings = new XmlWriterSettings(); 
    settings.Indent = true; 
    settings.IndentChars = ""; //this seems to do nothing
    //settings.IndentChars = (ControlChars.Tab)
    settings.NewLineChars = "\r\n"; 
    settings.NewLineHandling = NewLineHandling.Replace;
    XmlWriter writer = XmlWriter.Create(sb, settings);
    doc.Save(writer);
    writer.Flush();
    writer.Close();
    }
    catch
    {
    MessageBox.Show("Could not beautify " + doc.BaseURI);
    }
    }
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,120
    Rep Power
    9398
    Are you sure the node doesn't have a value? Whitespace matters: <foo></foo> (aka <foo />) is not the same as
    <foo>
    </foo>
    .
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0
    Originally Posted by requinix
    Are you sure the node doesn't have a value? Whitespace matters: <foo></foo> (aka <foo />) is not the same as
    <foo>
    </foo>
    .
    the document I am passing in has nodes with no values or whitespace. i.e. I'm passing in things like <foo></foo>
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0
    No clues eh?

IMN logo majestic logo threadwatch logo seochat tools logo