I know that it doesn’t look like best practice but I need to generate xml with same namespaces
for example:
<ns1:root xsi:schemaLocation=""http://schemalocation""
xmlns:ns1=""http://schema""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns=""http://schema"">
...
</ns1:root>
I also added namespaces to serializer:
var xmlSerializerNamespaces = new XmlSerializerNamespaces();
xmlSerializerNamespaces.Add("ns1", "http://schema");
xmlSerializerNamespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlSerializerNamespaces.Add(string.Empty, "http://schema");
And this is class itself:
[XmlRoot(ElementName = "request", Namespace = "http://schema")]
[Serializable]
public class Request
{
[XmlAttributeAttribute("schemaLocation", Namespace = XmlSchema.InstanceNamespace)]
public string SchemaLocation
{
get { return _schemaLocation; }
set { _schemaLocation = value; }
}
...
private string _schemaLocation = "http://schemalocation"; }
So everything is great but default xmlns isn’t in generated xml.
I’ve also played with XmlWriterSettings with no result.
Have anybody ideas how to do it without string replacement?)
The default namespace is set based on which namespace you use to add the XML fragment e.g.
Would produce the following output:
So all you have to do is add your other named ones to the document i.e.