Setting XML Root XMLNS

Julio Bello 221 Reputation points
2023-09-19T12:11:16.19+00:00

Hi, Everybody!

I need to create a Shipment XML root whose xmlns is the following fictitious XSD URL: http://api.company.com/schemas/api/company-api-vN.xsd.

<?xml version="1.0" encoding="utf-8"?>
<Shipment
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://api.company.net/schemas/api/company-api-v0.4.xsd">
	<Account>ACCOUNT</Account>
	<SNIP />
</Shipment>

I thought that setting its class XMLRoot attribute Namespace to "http://api.company.com/schemas/api/company-api-vN.xsd" would resolve the matter.

    [XmlRoot(ElementName = "Shipment", Namespace = "http://api.company.net/schemas/api/company-api-v0.4.xsd")]
    public partial class Shipment
    {
        [XmlElement(ElementName = "Account")]
        public string Account { get; set; }
        // <SNIP />
    }

Instead, it sets its children's XML tags xmlns attribute accordingly.

<Shipment
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<Account
		xmlns="http://api.company.com/schemas/api/company-api-vN.xsd">ACCOUNT
	</Account>
	<SNIP />
</Shipment>

The following code generates the XML from the Shipment object.

        private string ObjectToXmlString<TRequest>(TRequest request)
        {
            var memoryStream = new MemoryStream();
            var xmlSerializer = new XmlSerializer(typeof(TRequest));
            var streamReader = new StreamReader(memoryStream);

            xmlSerializer.Serialize(memoryStream, request);
            memoryStream.Position = 0;

            var xmlString = streamReader.ReadToEnd();

            return xmlString;
        }

How do I set the Shipment XML root's xmlns to the desired fictitious XSD URL?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,798 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,843 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,152 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,416 Reputation points Microsoft Vendor
    2023-09-22T09:02:06.25+00:00

    Hi,@Julio Bello. You could try to see if the following code is what you want.

    
    public class Program
    {
        public static void Main()
        {
    
          
            Shipment test = new Shipment();
            test.Account = "ACCOUNT";
    
            MyWrapper wrapper = new MyWrapper
            {
                Items = { test }
            };
            SerializeToXML(wrapper);
        }
        static public void SerializeToXML(MyWrapper list)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper));
            using (TextWriter textWriter = new StreamWriter(@"C:\Users\Administrator\Desktop\test.xml"))
            {
                serializer.Serialize(textWriter, list);
                textWriter.Close();
            }
        }
    
        private static string ObjectToXmlString<TRequest>(TRequest request, string xmlns)
        {
            var memoryStream = new MemoryStream();
            var xmlSerializer = new XmlSerializer(typeof(TRequest));
            var streamWriter = new StreamWriter(memoryStream);
    
            // Create XmlSerializerNamespaces to specify the namespaces
            var namespaces = new XmlSerializerNamespaces();
            namespaces.Add("", xmlns); // Default namespace
                     namespaces.Add("", "http://api.company.com/schemas/api/company-api-vN.xsd");
            xmlSerializer.Serialize(streamWriter, request, namespaces);
            memoryStream.Position = 0;
    
            var xmlString = new StreamReader(memoryStream).ReadToEnd();
    
            return xmlString;
        }
       
    
    }
    [XmlRoot("Shipment", Namespace = "http://api.company.com/schemas/api/company-api-vN.xsd")]
    public class MyWrapper
    {
        private List<Shipment> items = new List<Shipment>();
        [XmlElement("Account")]
        public List<Shipment> Items { get { return items; } }
    }
    
    
    
    [XmlRoot(ElementName = "Shipment")   ]
    public partial class Shipment
    {
        [XmlElement(ElementName = "Account")]
        public string Account { get; set; }
        
    }
    
    
    
    
    

    The result:

    1


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.