Re: Help Creating XmlNode / XmlAttribute Elegantly
From: Derek Harmon (loresayer_at_msn.com)
Date: 09/03/04
- Next message: Derek Harmon: "Re: Order of serialization of attributes"
- Previous message: Mike Lansdaal: "Re: Getting exception: Cannot have '?>' inside an XML processing"
- In reply to: David Elliott: "Help Creating XmlNode / XmlAttribute Elegantly"
- Next in thread: Dave: "Re: Help Creating XmlNode / XmlAttribute Elegantly"
- Reply: Dave: "Re: Help Creating XmlNode / XmlAttribute Elegantly"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Derek Harmon: "Re: Order of serialization of attributes"
- Previous message: Mike Lansdaal: "Re: Getting exception: Cannot have '?>' inside an XML processing"
- In reply to: David Elliott: "Help Creating XmlNode / XmlAttribute Elegantly"
- Next in thread: Dave: "Re: Help Creating XmlNode / XmlAttribute Elegantly"
- Reply: Dave: "Re: Help Creating XmlNode / XmlAttribute Elegantly"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|