469,625 Members | 911 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

How to create element in namespace

Hi all i've to create a xml doc like this

<?xml version="1.0" encoding="UTF-8"?>
<Main xmlns:x="http://www.w3.org/1999/XML/xinclude">
<x:include href="one.xml"/>
<x:include href="two.xml"/>
<x:include href="more.xml"/>
</Main>

i tried to use xmlnamespacemanager and also to declare the namespace
when create node but always get error namespace x not declared.

Please help me!

this my code:

XmlDocument doc=new XmlDocument();

XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8",
null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);

XmlNamespaceManager xmlNs=new XmlNamespaceManager(doc.NameTable);
xmlNs.AddNamespace("x", "http://www.w3.org/2001/XInclude");
xmlNs.PopScope();
XmlNode
main=doc.CreateNode(XmlNodeType.Element,"Main","ht tp://www.w3.org/2001/XInclude");
doc.AppendChild(main);

main.InnerXml=@"<xi:include href=""" + "one.xml" + @"""/>";

Apr 21 '06 #1
4 4423


Fa********@gmail.com wrote:
Hi all i've to create a xml doc like this

<?xml version="1.0" encoding="UTF-8"?>
<Main xmlns:x="http://www.w3.org/1999/XML/xinclude">
<x:include href="one.xml"/>
<x:include href="two.xml"/>
<x:include href="more.xml"/>
</Main>


You can use an XmlTextWriter like this

XmlTextWriter xmlWriter = new XmlTextWriter("file.xml", Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement("Main");
xmlWriter.WriteAttributeString("xmlns", "xinclude",
"http://www.w3.org/2000/xmlns/", "http://www.w3.org/1999/XML/xinclude");

xmlWriter.WriteStartElement("x:xinclude",
"http://www.w3.org/1999/XML/xinclude");
xmlWriter.WriteAttributeString("href", "one.xml");
xmlWriter.WriteEndElement();

xmlWriter.WriteStartElement("x:xinclude",
"http://www.w3.org/1999/XML/xinclude");
xmlWriter.WriteAttributeString("href", "two.xml");
xmlWriter.WriteEndElement();

xmlWriter.WriteStartElement("x:xinclude",
"http://www.w3.org/1999/XML/xinclude");
xmlWriter.WriteAttributeString("href", "more.xml");
xmlWriter.WriteEndElement();

xmlWriter.WriteEndDocument();
xmlWriter.Close();

Xml(Text)Writer is the preferred API to create XML.

Let us know if you really need to use XmlDocument and still need help
with that.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 21 '06 #2

Martin Honnen wrote:
Fa********@gmail.com wrote:
Hi all i've to create a xml doc like this
Let us know if you really need to use XmlDocument and still need help
with that.


HI, thanks, i'm writting an application reading from db and creating
xml document, i think i can use textwrite as well, however also i'd
like to know how to use XmlDocument to achieve this goal...

thanks again

Apr 21 '06 #3


Fa********@gmail.com wrote:

i'm writting an application reading from db and creating
xml document, i think i can use textwrite as well, however also i'd
like to know how to use XmlDocument to achieve this goal...


Ok, first a corrected version of the XmlTextWriter example (as I think
in the first answer I messed up local names and prefixes and the result
was not as in your original request)

XmlTextWriter xmlWriter = new XmlTextWriter("file.xml", Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement("Main");
xmlWriter.WriteAttributeString("xmlns", "x",
"http://www.w3.org/2000/xmlns/", "http://www.w3.org/1999/XML/xinclude");

xmlWriter.WriteStartElement("include",
"http://www.w3.org/1999/XML/xinclude");
xmlWriter.WriteAttributeString("href", "one.xml");
xmlWriter.WriteEndElement();

xmlWriter.WriteStartElement("include",
"http://www.w3.org/1999/XML/xinclude");
xmlWriter.WriteAttributeString("href", "two.xml");
xmlWriter.WriteEndElement();

xmlWriter.WriteStartElement("include",
"http://www.w3.org/1999/XML/xinclude");
xmlWriter.WriteAttributeString("href", "more.xml");
xmlWriter.WriteEndElement();

xmlWriter.WriteEndDocument();
As for doing it with XmlDocument, here is an example

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.AppendChild(xmlDocument.CreateElement( "Main"));
XmlAttribute attribute = xmlDocument.CreateAttribute("xmlns", "x",
"http://www.w3.org/2000/xmlns/");
attribute.Value = "http://www.w3.org/1999/XML/xinclude";
xmlDocument.DocumentElement.SetAttributeNode(attri bute);

XmlElement element = xmlDocument.CreateElement("x:include",
"http://www.w3.org/1999/XML/xinclude");
element.SetAttribute("href", "one.xml");
xmlDocument.DocumentElement.AppendChild(element);

element = xmlDocument.CreateElement("x:include",
"http://www.w3.org/1999/XML/xinclude");
element.SetAttribute("href", "two.xml");
xmlDocument.DocumentElement.AppendChild(element);

element = xmlDocument.CreateElement("x:include",
"http://www.w3.org/1999/XML/xinclude");
element.SetAttribute("href", "two.xml");
xmlDocument.DocumentElement.AppendChild(element);

// now save somewhere e.g.
xmlDocument.Save(Console.Out);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 21 '06 #4

Martin Honnen wrote:
Ok, first a corrected version of the XmlTextWriter example (as I think
in the first answer I messed up local names and prefixes and the result
was not as in your original request)

XmlTextWriter xmlWriter = new XmlTextWriter("file.xml", Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement("Main");
xmlWriter.WriteAttributeString("xmlns", "x",
"http://www.w3.org/2000/xmlns/", "http://www.w3.org/1999/XML/xinclude");

xmlWriter.WriteStartElement("include",
"http://www.w3.org/1999/XML/xinclude");
xmlWriter.WriteAttributeString("href", "one.xml");
xmlWriter.WriteEndElement();

xmlWriter.WriteStartElement("include",
"http://www.w3.org/1999/XML/xinclude");
xmlWriter.WriteAttributeString("href", "two.xml");
xmlWriter.WriteEndElement();

xmlWriter.WriteStartElement("include",
"http://www.w3.org/1999/XML/xinclude");
xmlWriter.WriteAttributeString("href", "more.xml");
xmlWriter.WriteEndElement();

xmlWriter.WriteEndDocument();
As for doing it with XmlDocument, here is an example

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.AppendChild(xmlDocument.CreateElement( "Main"));
XmlAttribute attribute = xmlDocument.CreateAttribute("xmlns", "x",
"http://www.w3.org/2000/xmlns/");
attribute.Value = "http://www.w3.org/1999/XML/xinclude";
xmlDocument.DocumentElement.SetAttributeNode(attri bute);

XmlElement element = xmlDocument.CreateElement("x:include",
"http://www.w3.org/1999/XML/xinclude");
element.SetAttribute("href", "one.xml");
xmlDocument.DocumentElement.AppendChild(element);

element = xmlDocument.CreateElement("x:include",
"http://www.w3.org/1999/XML/xinclude");
element.SetAttribute("href", "two.xml");
xmlDocument.DocumentElement.AppendChild(element);

element = xmlDocument.CreateElement("x:include",
"http://www.w3.org/1999/XML/xinclude");
element.SetAttribute("href", "two.xml");
xmlDocument.DocumentElement.AppendChild(element);

// now save somewhere e.g.
xmlDocument.Save(Console.Out);


Many thanks, you really give me a great help!!!

Apr 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gordon Dickens | last post: by
4 posts views Thread by Krishna Tulasi via .NET 247 | last post: by
reply views Thread by DotDidIt | last post: by
16 posts views Thread by TT (Tom Tempelaere) | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.