Dominik

Dominik

  • NA
  • 103
  • 0

Write an XML with namespaces.

Aug 13 2009 6:48 PM
Hi people,

I got a problem and I hope somebody can help me, because I can´t find the solution by myself.
I want to write an XML File with Csharp and the file needs to begin like that:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Comprobant
             xmlns="http://link1">
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="
                 http://www.link2
                  http://www.link3"
 <Emiss>
  <street>mystreet</street>
 </Emiss>
</Comprobant>

My code is the following:
using System;
using System.Data;
using System.Text;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Diagnostics;


namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            // XmlWriter for an UTF-8-Coding with indent
            XmlWriter xmlWriter = null;
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = " ";
            //Filename of the XML File
            string appPath = Path.GetDirectoryName(
            Assembly.GetEntryAssembly().Location);
            string xmlFileName = Path.Combine(appPath, "test.xml");
            xmlWriter = XmlWriter.Create(xmlFileName, settings);
            // standalone
            xmlWriter.WriteStartDocument(true);
            // Startelement with namespace
            xmlWriter.WriteStartElement("Comprobant",
               "http://link1");
            xmlWriter.WriteStartElement("Emiss"); //Element name
            xmlWriter.WriteElementString("street", "mystreet");
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
            // show xml
            Process.Start(xmlFileName);
        }
  
    }
}

And my XML that is generated is:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Comprobant xmlns="http://link1">
 <Emiss>
  <street>mystreet</street>
 </Emiss>
</Comprobant>

I don´t know what to do. Please help me out ..
How can I add these lines: ??????
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="
                 http://www.link2
                  http://www.link3"



Greetings Dom

Answers (2)