0

I am attempting to write the following line with C#'s XmlWriter.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">

I seem to only seem have an option to write on xmlns (and then with no :suffix), when using the xmlWriter.WriteStartElement method.

Is it possible to do with XmlWriter?

2
  • have you tried XmlWriter.WriteStartElement Method (String, String, String) ? Commented Jun 18, 2015 at 18:13
  • Yes, but it doesn't work. Attribute 1 looks for a "prefix" which gets added before the element name and after the xmlns: This obviously doesn't work. Nor does it allow you to specify two xmlns attributes. Commented Jun 18, 2015 at 22:02

1 Answer 1

1

Would this help? Adjusted to use LookupPrefix.

writer = XmlWriter.Create(sw);
writer.WriteStartElement("configuration");
writer.WriteAttributeString("xmlns", "patch", null, "http://www.sitecore.net/xmlconfig/");
writer.WriteAttributeString("xmlns", "set", null, "http://www.sitecore.net/xmlconfig/set/");
writer.WriteEndElement();
writer.Flush();
writer.Close();

outputs:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" />
Sign up to request clarification or add additional context in comments.

3 Comments

But since both need to be preceded by xmlns: that wouldn't work. I would end up with <configuration xmlns:patch="sitecore.net/xmlconfig" xmlns:xmlns="namespace" /> which obviously won't work, since that last "xmlns" should say "set" instead. Oh yeah, and the second one's value should be another URL.
@eat-sleep-code Tweeked it a bit. looking up the prefix seems to function better.
My bad, didn't see the requirement for xmlns, seems closer though.

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.