elchente23
elchente23

Reputation: 183

Serialize XML with different prefix

I got 2 classes from 2 different XSDs, one of them is its child node, the root class has a property (xmlelement array) for the child and I need the child node to have a different prefix. This is my code:

var xml = //this is the root xml
var nom = //this is the child node

var stream = new MemoryStream();

var xmlSerializeNomina = new XmlSerializer(typeof(ChildClass));

var xmlNameSpaceNom = new XmlSerializerNamespaces();

//Here add namespace because i need the prefix in child node
xmlNameSpaceNom.Add("childPrefix", "http://www.url.com/child");
var doc = new XmlDocument();
using (var writer = new XmlTextWriter(stream, Encoding.UTF8))
{
    writer.Formatting = Formatting.Indented;

    xmlSerializeNomina.Serialize(writer, nom, xmlNameSpaceNom);

    stream.Seek(0, SeekOrigin.Begin);

    doc.Load(stream);

}
//This is the xmlelement array property
xml.Complemento.Any = new XmlElement[] { doc.ImportNode(doc.DocumentElement, true) as XmlElement };

Then I serialize the root:

var xmlNameSpace = new XmlSerializerNamespaces();

xmlNameSpace.Add("rootPrefix", "http://www.url.com/foo");
xmlNameSpace.Add("nsPrefix", "http://www.url.com/foo");
xmlNameSpace.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

var urls += "http://www.url.com/foo http://www.url.com/root.xsd http://www.url.com/url_child http://www.url.com/url_child/child.xsd";

//Here add then child namespace because i need it in root node    
xmlNameSpace.Add("childPrefix", "http://www.url.com/child");

xmlNameSpace.Add("schemaLocation", urls);

var xmlSerializeFactura = new XmlSerializer(typeof(RootClass));

using (var xmlTextWriter = new XmlTextWriter("pathAndName.xml", Encoding.UTF8))
{
    xmlTextWriter.Formatting = Formatting.Indented;

    xmlSerializeFactura.Serialize(xmlTextWriter, xml, xmlNameSpace);
}

The XML file was created in the right format, but the child root has the namespace and I don't need it there, only in the root node.

<?xml version="1.0" encoding="utf-8"?>
<rootPrefix:Comprobante xmlns:schemaLocation="http://www.url.com/foo http://www.url.com/root.xsd http://www.url.com/child http://www.url.com/child/child.xsd" xmlns:nsPrefix="http://www.url.com/foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.url.com/foo http://www.url.com/root.xsd" xmlns:rootPrefix="http://www.url.com/foo">
  <rootPrefix:tags>
    Some more tags...
  </rootPrefix:tags
  <rootPrefix:Complemento>
    '<!--' in this part is added the namespace, i don't need it here because i have already on root tag but if I don't add it this child tag haven't prefix  '-->'
    <childPrefix:Tag xmlns:childPrefix="http://www.url.com/child">
      Some childs tags..
    </childPrefix:Tag>
  </rootPrefix:Complemento>
</rootPrefix:Comprobante>

Upvotes: 1

Views: 1551

Answers (1)

Alexander Petrov
Alexander Petrov

Reputation: 14231

Do it simple

[XmlRoot(Namespace = "http://www.url.com/foo")]
public class Comprobante
{
    public string Tags { get; set; }
    public Complemento Complemento { get; set; }
}

[XmlType(Namespace = "http://www.url.com/foo")]
public class Complemento
{
    [XmlElement(Namespace = "http://www.url.com/child")]
    public string Tag { get; set; }
}
var child = new Complemento { Tag = "tag" };
var root = new Comprobante { Tags = "tags", Complemento = child };

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("rootPrefix", "http://www.url.com/foo");
ns.Add("childPrefix", "http://www.url.com/child");

var xs = new XmlSerializer(typeof(Comprobante));

xs.Serialize(Console.Out, root, ns);

In the result, the child node would not have a namespace definition. As you want.

Upvotes: 2

Related Questions