I'm simply trying to make my XML a bit tidier and less bulky. I know in C# one can do something like this:
XNamespace ds = "http://schemas.microsoft.com/ado/2007/08/dataservices";
new XElement(ds + "MyDumbElementName", "SomethingStupid");
And get an XML simliar to this:
<root>
<MyDumbElementName xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
SomethingStupid
</MyDumbElementName>
</root>
Instead of something like this:
<root xmlns:ds="http://schemas.microsoft.com/ado/2007/08/dataservices">
<ds:MyDumbElementName>
SomethingStupid
</ds:MyDumbElementName>
</root>
Obviously the second version is much prettier, easier to read, and compact. Is there any way to generate an XDocument equivalent of the compact version, without calling Parse("...")?
You may decide to take a risk and answer "No", in which case I believe the fair thing to do is wait for other people to answer, and if no-one gives a decent answer I'll accept your "No", otherwise if someone does provide an answer, I'll mark the "No" down. I hope that seems fair to you too.
EDIT: Perhaps I should be a bit more specific and say that I want to be able to use multiple name spaces, not just one.
You can explicitly override this behaviour by specifying the xmlns attribute:
By default the behaviour is to specify the xmlns in-line.
Gives the output:
So the default behaviour is your desired behaviour, except when the namespace is already defined: