0

New here, looking to get a little help with my XmlDocument. Is it possible to have string data in my root element AND remove the xmlns= attribute from being shown? I'm looking for something like this:

<Rulebase author=yadda datetime=bingbang version=1.x </Rulebase>

When I try to use my string data by doing:

xmlDom.AppendChild(xmlDom.CreateElement("", "Rulebase", data));
XmlElement xmlRoot = xmlDom.DocumentElement;

It ends up looking like this:

<Rulebase xmlns="version=0 author=username date=7/13/2011 </Rulebase>

and it also appends xmlns="" to all my other nodes.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
MrJaxon
  • 5
  • 1
  • 2
  • I hope this is useful to you...[How to remove all namespaces from XML with C#?][1] [1]: http://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c – shenhengbin Jul 14 '11 at 00:53
  • 3
    Please post the actual XML. What you posted is not XML. – John Saunders Jul 14 '11 at 02:34

1 Answers1

3

The CreateElement overload you're using takes a prefix as it's first argument, local name as second, and namespace as third. If you don't want a namespace, don't use this overload. Just use the one that takes a local name as the one and only argument. Then add your data separately as child elements and attributes.

var xmlDom = new XmlDocument();
XmlElement root = xmlDom.CreateElement("Rulebase");
xmlDom.AppendChild(root);
XmlElement data = xmlDom.CreateElement("Data");
root.AppendChild(data);

XmlAttribute attribute = xmlDom.CreateAttribute("author");
attribute.Value = "username";
data.Attributes.Append(attribute);

attribute = xmlDom.CreateAttribute("date");
attribute.Value = XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.RoundtripKind);
data.Attributes.Append(attribute);

Console.WriteLine(xmlDom.OuterXml);

Creates (formatting added)

<Rulebase>
    <Data author="username" date="2011-07-13T22:44:27.5488853-04:00" />
</Rulebase>

Using XmlDocument to generate XML is pretty tedious though. There are many better ways in .NET, like XmlSerializer and DataContractSerializer. You can also use Linq-to-Xml and XElement. Or you can use an XmlWriter.Create(). Lots of options.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • -1 for mentioning `XmlTextWriter`, deprecated since .NET 2.0. – John Saunders Jul 14 '11 at 02:52
  • @John Saunders, technically, it's not deprecated, but I updated my answer to point to `XmlWriter` instead. – Samuel Neff Jul 14 '11 at 03:22
  • 1
    perhaps you mean that it isn't marked with `[Obsolete]`, but the fact that they say, "don't use `new XmlTextWriter()`, instead use `XmlWriter.Create()`" is the same IMO as "`new XmlTextWriter()` is deprecated in favor of `XmlWriter.Create()`" – John Saunders Jul 14 '11 at 03:26
  • @John Saunders, yes, that's why I updated my answer. Thanks for the comment. – Samuel Neff Jul 14 '11 at 03:39
  • many thanks, i ended up doing it the way you provided in the example. a little tedious but not a big deal. really appreciate all the responses! – MrJaxon Jul 14 '11 at 18:34