You want to control the XML prefix outputted. For reference an MSDN site
Basically you just need to add the xml:xsi
to your root node and Linq to XML should take care of the rest.
Note that when you get into really complex examples it tends to fall apart, but it should work in this case.
EDIT:
To remove extraneous attributes, you could simply do it by hand:
foreach(var element in root.Descendents())
{
foreach (var attribute in element.Attributes())
{
if (attribute.Name.Namespace == XNamespace.Xmlns)
attribute.Remove();
}
}
Note the above is rough, I don't have an XML project handy.
EDIT:
I am not sure what your input is, but here is an example of hard coding your expected output:
var xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
var fragment =
new XElement("Fragment",
new XAttribute(XNamespace.Xmlns + "p5", xsi.ToString()),
new XElement("CustomerRecord",
new XAttribute(xsi + "Exists", "true")),
new XElement("CustomerRecord",
new XAttribute(xsi + "Exists", "false")));
I tested this and it outputs identical to what you asked for (well I tested in F#, so sorry if there is a syntax error)