Re: Help Creating XmlNode / XmlAttribute Elegantly

From: Derek Harmon (loresayer_at_msn.com)
Date: 09/03/04


Date: Thu, 2 Sep 2004 20:39:19 -0400


"David Elliott" <Webbert@newsgroups.nospam> wrote in message news:2b5ej01vee1p82uoqo9a0os9g4alfia3g2@4ax.com...
> I am looking to see if there is a better way to add a
> new node / attribute than using XmlDocument.InnerXml.
: :
> but couldn't really see another way of doing this without
> defining namespaces and the like.

The example XML you've given doesn't contain any namespaces,
so why should you define one?

Have a look at the CreateElement() and CreateAttribute()
methods of XmlDocument, and then AppendChild() on XmlNode
(and remember that XmlDocument is an XmlNode, so it also
allows you to AppendChild() nodes to it).

> the lines that I am wondering about are lines: 79, 227,
> and 266

79: xmlDoc.InnerXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><configuration></configuration>";

If you set UTF-8 Encoding on an I/O Stream wrapped by an
XmlTextWriter (or TextWriter), this XML declaration will
be emitted for you automatically.

Here's how you'd create a custom XmlDeclaration for the
XmlDocument (note that if you do Save this XML to a
TextWriter-based class, its Encoding takes precedence).

    XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");

Then you'd want to create your <configuration> element.

    XmlElement eRoot = xmlDoc.CreateElement( "configuration");

Finally, add the root element to XmlDocument and insert
the XmlDeclaration before it,

    xmlDoc.AppendChild( eRoot);
    xmlDoc.InsertBefore( xmlDecl, eRoot);

: :
223: StringBuilder sb = new StringBuilder(500);
224: sb.Append("<add key=\""); sb.Append(key); sb.Append("\" ");
225: sb.Append("value=\""); sb.Append(keyValue); sb.Append("\" />");
226:
227: node.InnerXml += sb.ToString();

    XmlElement eAdd = xmlDoc.CreateElement( "add");
    eAdd.SetAttribute( "key", key);
    eAdd.SetAttribute( "value", keyValue);
    node.AppendChild( eAdd);

: :
264: sb.Append("</"); sb.Append(section); sb.Append(">");
265:
266: node.InnerXml += sb.ToString();

Your example code never gives a value to the argument,
section, but from your example XML I'll guess it's
<appSettings> (although I don't see where you have
inserted the child node between the start and end
elements).

    XmlElement eAppSettings = xmlDoc.CreateElement( "appSettings");
    eAppSettings.AppendChild( eAdd);
    eRoot.AppendChild( eAppSettings);

These three statements essentially tie together the
entire configuration document's XML nodes.

It looks like you own (because you want to own?) the
entire configuration file, but I'll also point out that
in the .NET Framework's System.Configuration namespace
are classes automatically supporting this configuration
schema to read a config file from "application.exe.config"
or "web.config".

Using the built-in configuration file support allows you
to create the element,

    <add name="key" value="keyValue" />

in a single statement like this,

    ConfigurationSettings.AppSettings[ key] = keyValue;

with the default NameValueSectionHandler.

Derek Harmon



Relevant Pages

  • Re: Help Creating XmlNode / XmlAttribute Elegantly
    ... "Derek Harmon" wrote: ... > The example XML you've given doesn't contain any namespaces, ... > methods of XmlDocument, ... > Using the built-in configuration file support allows you ...
    (microsoft.public.dotnet.xml)
  • Re: Editting an XML file
    ... I have an overriden XmlDocument. ... A whole segment of my XML is disappearing during my save. ... Here is an example how you could build your own method to find child ...
    (microsoft.public.dotnet.xml)
  • Re: Code review comment: "Noone uses tokens anymore"
    ... >> XML or binary for data files, ... sscanfdoesn't have any parsing capabilities that are useful ... the configuration file is pretty simple. ... If you did this explicitely as tokenization, then parsing, then that's ...
    (comp.programming)
  • Re: Convert String data from Web Service to XML
    ... Are you using Xlang or external .net assembly to load xml? ... XmlDocument temp2 = ... why not use temp instead of temp2, ... I am trying to Convert String data from Web Service to XML ...
    (microsoft.public.biztalk.general)
  • Re: XPathNavigator.SelectSingleNode(xpath) on space returns 0 leng
    ... But I can use the Stream to create an XmlDocument, ... why does XPathDocument eat all of the whitespace? ... Because xml schemes like Excel's SpreadsheetML need it. ... XmlDocument without preserveWhitespace, ...
    (microsoft.public.dotnet.xml)