0

I'm trying to use LINQ to XML to write queries for a WebDAV server but I'm having a problem where LINQ is setting a default namespace (xmlns = "bla") which doesn't seem to be supported by WebDAV.

XNamespace ns = "d";
var content = new XElement(ns + "propfind"
,new XAttribute(XNamespace.Xmlns + "d", "DAV:")
,new XElement(ns + "allprops"));

The expected output is:

<d:propfind xmlns:d="DAV:"><d:allprop /></d:propfind>

But no matter how I attempt to serialize (even with XElement.Save(someStream, SaveOptions.DisableFormatting)) I always get this which is not supported by the WebDav server I'm trying to hit.

<propfind xmlns:d="DAV:" xmlns="d"><allprop /></propfind>

1 Answer 1

3

The problem is that your element isn't in the DAV: namespace - it's in the d namespace. You need to differentiate between the namespace URI and the namespace alias. You want:

XNamespace ns = "DAV:";
var content = new XElement(ns + "propfind",
    new XAttribute(XNamespace.Xmlns + "d", ns),
    new XElement(ns + "allprops"));
Sign up to request clarification or add additional context in comments.

3 Comments

Ahh ok thank you. So if I'm understanding correctly I'm still using the namespace 'DAV' in all my definitions and the serialization will understand that it should use the alias I'm providing as 'd'
@NtscCobalt: Yes - although it will also be the default for the element and descendants, so unless you want the alias, just remove the attribute entirely...
Ok makes sense. Yes in this case I need the alias. IIS gives me a 400 if DAV is set as the default namespace on the root element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.