How to get Custom Namespaces and Prefix for an XML file?

TRAIAN MACAVEIU 91 Reputation points
2024-04-17T11:23:54.3966667+00:00

I have a structure in which I fill in data and I want to save it as an XML file. How can I set Namespaces and Prefix so as to obtain an XML file with the structure as in the example below? Thank you!

public class AuditFile
{
	public Header Header { get; set; }
}	

public class Header
{			
	public string AuditFileVersion { get; set; }
	public string AuditFileCountry { get; set; }
	public string AuditFileRegion { get; set; }
}

fill and save:

AuditFile doc = new AuditFile();
Header header = new Header();
//fill with data
doc.Header = header;

XmlSerializer x = new System.Xml.Serialization.XmlSerializer(doc.GetType());
TextWriter writer = new StreamWriter(@"C:\Docs\document.xml");
x.Serialize(writer, doc);
writer.Close();

desired XML structure:

<?xml version="1.0" encoding="UTF-8"?>
<nsSAFT:AuditFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:nsSAFT="mfp:anaf:dgti:d406:declaratie:v1" xsi:schemaLocation="urn:StandardAuditFile-Taxation-Financial:RO schema.xsd">
	<nsSAFT:Header>
		<nsSAFT:AuditFileVersion>1.0</nsSAFT:AuditFileVersion>				
<nsSAFT:AuditFileCountry>RO</nsSAFT:AuditFileCountry>
	</nsSAFT:Header>
</nsSAFT:AuditFile>	
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,383 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,260 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 31,981 Reputation points Microsoft Vendor
    2024-04-18T07:48:00.83+00:00

    Hi @TRAIAN MACAVEIU , Welcome to Microsoft Q&A,

    You want to add the namespace prefix nsSAFT: to each node to ensure that the entire XML structure uses that prefix. To achieve this, you need to explicitly add the namespace prefix when generating the XML file. To generate XML files with a specific namespace and prefix, you can use the XmlSerializerNamespaces class to specify the namespace and prefix.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace xx
    {
    
    public class AuditFile
    {
        [XmlElement(Namespace = "mfp:anaf:dgti:d406:declaratie:v1")]
        public Header Header { get; set; }
    }
    
    public class Header
    {
        public string AuditFileVersion { get; set; }
        public string AuditFileCountry { get; set; }
    }
    
        internal class Program
        {
            static void Main(string[] args)
            {
            AuditFile doc = new AuditFile();
            Header header = new Header();
            // Fill with data
            header.AuditFileVersion = "1.0";
            header.AuditFileCountry = "RO";
            doc.Header = header;
    
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("nsSAFT", "mfp:anaf:dgti:d406:declaratie:v1");
            namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    
            XmlSerializer x = new XmlSerializer(typeof(AuditFile));
    
            // Configure XmlWriter to add namespace prefixes
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
    
            using (XmlWriter writer = XmlWriter.Create(@"C:\Users\Administrator\Desktop\demo.xml", settings))
            {
                writer.WriteStartElement("nsSAFT", "AuditFile", "mfp:anaf:dgti:d406:declaratie:v1");
                writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
                writer.WriteAttributeString("xsi", "schemaLocation", null, "urn:StandardAuditFile-Taxation-Financial:RO schema.xsd");
    
                // Serialize the whole AuditFile object
                x.Serialize(writer, doc, namespaces);
    
                writer.WriteEndElement();
            }
        }
            }
        }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful