Include Default XML namespace

Asked

Viewed 49 times

1

Srs(as), good morning,

I am generating an XML from the serialization of a class.Cs(schema) and the output is created xml (first example)

.

This will be exported to an external system, however they asked to leave the XML equal to (second example).

From what I’ve seen, there’s a Namespace Defaut (tns) that I would need to include, but I can’t do it.

XML is being generated by the code below.

entitiesList = new extEntitiesList() { entity = tEntity };
                    fileName = string.Format("{0}{1}{2}", sPath, tEntity.idExt, ".xml");
                    serializer = new System.Xml.Serialization.XmlSerializer(typeof(extEntitiesList));                   

                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName, false, encoding))
                    {
                        serializer.Serialize(sw, entitiesList);
                        //sw.Write(xml.InnerXml);
                        sw.Dispose();
                    }

The class was generated by the XSD.exe VS2010 command. spawned:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://value.com/model/v2/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://value.com/model/v2/", IsNullable=false)]

(first example)

    <?xml version="1.0" encoding="iso-8859-1"?>
<extEntitiesList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://value.com/model/v2/">
  <entity>
    <idExt>value</idExt>
    <name>value</name>
    <fntsyName>value</fntsyName>
    <personType>value</personType>
    <taxId>value</taxId>
    <inscMun />
    <clientType>
      <name>Ivalue</name>
      <id>
        <value>value</value>
      </id>
    </clientType>
    <platform>
      <platformId />
      <name />
      <email />
    </platform>
    <economicGroup>
      <name>value</name>
      <id>
        <value>value</value>
      </id>
    </economicGroup>
    <obs />
    <obsWithoutPwrImpact />
    <address>
      <state>value</state>
      <city>value</city>
      <publicArea>value</publicArea>
      <comp>value</comp>
      <zip>value</zip>
      <phoneNr>value</phoneNr>
      <faxNr>value</faxNr>
    </address>
    <lob>
      <extId />
      <name />
    </lob>
  </entity>
</extEntitiesList>

(second example)

<?xml version="1.0" encoding="UTF-8"?>
<tns:extEntitiesList xmlns:tns="http://value.com/model/v2/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://value.com/model/v2/ value.xsd ">
<tns:entity>
    <tns:idExt>value</tns:idExt>    
    <tns:name>value</tns:name>
    <tns:fntsyName>value</tns:fntsyName>
    <tns:personType>value</tns:personType>
    <tns:taxId>value</tns:taxId>
    <tns:inscEst>value</tns:inscEst>
    <tns:inscMun>value</tns:inscMun>
    <tns:clientType>
      <tns:name>value</tns:name>
      <tns:id>
        <tns:value>value</tns:value>
      </tns:id>
    </tns:clientType>
       <tns:responsibleArea>
      <tns:respAreaId>value</tns:respAreaId>
      <tns:name>value</tns:name>
      <tns:email>value</tns:email>
    </tns:responsibleArea>
    <tns:platform>
      <tns:platformId>value</tns:platformId>
      <tns:name>value</tns:name>
      <tns:email>value</tns:email>
    </tns:platform>
    <tns:economicGroup>
      <tns:name>value</tns:name>
      <tns:id>
        <tns:value>value</tns:value>
      </tns:id>
    </tns:economicGroup>
    <tns:expirationDate>value</tns:expirationDate>
    <tns:status>value</tns:status>  
    <tns:document>
      <tns:documentExtId>value</tns:documentExtId>
      <tns:documentTypeExtId>value</tns:documentTypeExtId>
      <tns:documentTypeName>value</tns:documentTypeName>
      <tns:formalizationId>value</tns:formalizationId>
      <tns:formalizationName>value</tns:formalizationName>
      <tns:book>value</tns:book>
      <tns:sheet>value</tns:sheet>
      <tns:issueDate>value</tns:issueDate>
      <tns:pubDate>value</tns:pubDate>
      <tns:expirDate>value</tns:expirDate>
      <tns:grantDate>value</tns:grantDate>
      <tns:closeDate>value</tns:closeDate>
      <tns:active>value</tns:active> 
    </tns:document>
    <tns:address>
      <tns:addressExtId>value</tns:addressExtId>
      <tns:state>value</tns:state>
      <tns:city>value</tns:city>
      <tns:publicArea>value</tns:publicArea>
      <tns:number>value</tns:number>
      <tns:comp>value</tns:comp>
      <tns:district>value</tns:district>
      <tns:zip>value</tns:zip>
      <tns:phoneNr>value</tns:phoneNr>
      <tns:faxNr>value</tns:faxNr>
    </tns:address>
    <tns:lob>
      <tns:extId>value</tns:extId>
      <tns:name>value</tns:name>
    </tns:lob>
    </tns:entity>
    <tns:entity>
</tns:extEntitiesList>
  • It is interesting to post the code you are using to generate xml.

  • Included... Thank you!!!

1 answer

1


Luiz,

Try using Xmlserializernamespaces before serialization:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("tns", "http://value.com");
XmlSerializer serializer = new XmlSerializer(typeof(extEntitiesList));
serializer.Serialize(Console.Out, new extEntitiesList(), ns);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.