使用不同的前缀序列化XML

时间:2016-07-02 04:04:47

标签: c# xml xml-namespaces xmlserializer

我从2个不同的xsd获得了2个类,其中一个是它的子节点,根类有一个属性(xmlelement数组),我需要子节点有不同的前缀,这是我的代码:

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 };

然后我序列化根:

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);
}

xml文件是以正确的格式创建的,但是子root具有命名空间,我不需要在那里,只在根节点中。

<?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>

1 个答案:

答案 0 :(得分:1)

做到这一点

[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);

在结果中,子节点没有命名空间定义。如你所愿。