How do I add the xsi and xmlns:xsi tags to an XML file using Linq in C#?

I've never quite understood this and am struggling again.  I need my XML file to look like this:

<?xml version="1.0"?>

-<eTest xsi:noNamespaceSchemaLocation="eTest-Schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+<GLEntries>
</eTest>

I am using this technique:

                    XDocument ardoc = new XDocument();
                    XElement eTest= new XElement("eTest");
                    ardoc.Add(eTest);
                    ardoc.Save(this._ARExpPath + "AR" + dt.Date.ToString("yyyyMMdd") + ".XML");

I don't know how to get the xsi and xmlns:xsi tags (attributes?) into the eTest element.

Thanks.
LVL 4
g_johnsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi g_johnson;

I have modified your code to add the XNamespace's
// Create the XNamespace's you need which will be added to the Root node
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace noNamespaceSchemaLoc = XNamespace.Get("eTest-Schema.xsd");

XDocument ardoc = new XDocument();
XElement eTest = new XElement("eTest", 
    new XAttribute(xsi + "noNamespaceSchemaLoc", noNamespaceSchemaLoc.NamespaceName),
    new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName));

ardoc.Add(eTest);

ardoc.Save(this._ARExpPath + "AR" + dt.Date.ToString("yyyyMMdd") + ".XML");

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
You can also setup a type serializer to do this:
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace EE_Q28736062
{
	class Program
	{
		static void Main(string[] args)
		{
			ETestSerializer test = new ETestSerializer();
			test.GenerateXml("test.xml");
		}
	}

	[Serializable(), XmlRoot("eTest")]
	public class ETestSerializer
	{
		[XmlAttribute(Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
		public string noNamespaceSchemaLocation = "eTest-Schema.xsd";

		public void GenerateXml(string fileName)
		{
			GenerateXml(new FileInfo(fileName));
		}

		public void GenerateXml(FileInfo file)
		{
			XmlSerializer serializer = new XmlSerializer(GetType());
			using (TextWriter stream = new StreamWriter(file.FullName))
			{
				XmlSerializerNamespaces spaces = new XmlSerializerNamespaces();
				spaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
				serializer.Serialize(stream, this, spaces);
			}
		}
	}
}

Open in new window

Which produces the following xml file -Capture.JPG-saige-
1
it_saigeDeveloperCommented:
And probably a little more than you were expecting but here is one way this method could be utilized in conjunction with your GLEntries (just making up an implementation):
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace EE_Q28736062
{
	class Program
	{
		static void Main(string[] args)
		{
			ETestSerializer test = new ETestSerializer();
			test.GLEntries.Add(new GLEntry() { ID = 1, Name = "First" });
			test.GenerateXml("test.xml");
		}
	}

	[Serializable(), XmlRoot("eTest")]
	public class ETestSerializer
	{
		private readonly List<GLEntry> fGLEntries = new List<GLEntry>();

		[XmlAttribute(Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
		public string noNamespaceSchemaLocation = "eTest-Schema.xsd";

		[XmlElement()]
		public List<GLEntry> GLEntries { get { return fGLEntries; } }

		public void GenerateXml(string fileName)
		{
			GenerateXml(new FileInfo(fileName));
		}

		public void GenerateXml(FileInfo file)
		{
			XmlSerializer serializer = new XmlSerializer(GetType());
			using (TextWriter stream = new StreamWriter(file.FullName))
			{
				XmlSerializerNamespaces spaces = new XmlSerializerNamespaces();
				spaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
				serializer.Serialize(stream, this, spaces);
			}
		}
	}

	[Serializable()]
	public class GLEntry
	{
		public int ID { get; set; }
		public string Name { get; set; }
	}
}

Open in new window

Which now produces the following output -Capture.JPG-saige-
0
g_johnsonAuthor Commented:
Thanks.
0
Fernando SotoRetiredCommented:
Not a problem g_johnson, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.