Well, whenever you do a WriteRaw, you're telling it to write a raw string
and it doesn't apply your indentation settings. What you're looking for is
XmlTextWriter.WriteNode

XmlNode n = XXX;
writer.WriteNode( new XmlTextReader(n.OuterXml, XmlNodeType.Element, null),
true );

Adam..

-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Jekke Bladt
Sent: Tuesday, March 15, 2005 10:56 AM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] XML formatting when using a FileStream /
XmlTextWriter

All--

For some reason, when I use XmlTextWriter.Formatting =
Formatting.Indented and try to write to a FileStream, the XML is coming
out as an undifferentiated blob, valid and machine readable, but very
hard to edit by hand.

I'm using the code listed below and getting these results. Can anyone
tell me how to get formatted and indented XML within this context?

TIA

        public static bool SaveSettings()
        {
                Stream theStream = null;
                XmlTextWriter xw;
                DirectoryEntry myADObject = null;

                if(_DebugMode)
                {
                        // XML File
                        StringBuilder settingPath = new
StringBuilder(System.Environment.CurrentDirectory);
                        settingPath.Append("\\");
                        settingPath.Append(CurrentUserLogin());
                        settingPath.Append(".xml");

                        theStream = new
FileStream(settingPath.ToString(), FileMode.Create);
                }
                else
                {
                        // Active Directory
                        myADObject = GetMyActiveDirectoryNode();
                        theStream = new MemoryStream();
                }

                // Use each setting's serialize method to get the
correct XML.
                xw = new XmlTextWriter(theStream, Encoding.ASCII);
                xw.Formatting = System.Xml.Formatting.Indented;
                xw.Indentation = 2;

                xw.WriteStartElement("trader-extendedsettings");
                SortedList valuesList = Tools.ToSortedList(_Values);
                foreach(DictionaryEntry thisEntry in valuesList)
                {
                        // Loop through each object in the hash table,
retrieve its XML
                        // through the Serialize method, and add it to
the settings "document."
                        IGroupOneSetting thisSetting =
(IGroupOneSetting)thisEntry.Value;
                        XmlNode serializedData =
thisSetting.Serialize();
                        string outerXML = serializedData.OuterXml;
                        xw.WriteRaw(outerXML);
                }

                xw.WriteEndElement();
                xw.Flush();

                if(!_DebugMode)
                {
                        StreamReader sr = new StreamReader(theStream);
                        theStream.Seek(0, SeekOrigin.Begin);
                        string theSettings = sr.ReadToEnd();

myADObject.Properties["ExtendedTraderInformation"].Value =
(object)theSettings;
                        myADObject.CommitChanges();
                }

                xw.Close();

                return true;
        }

--Jekke

===================================
This list is hosted by DevelopMentor.  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to