Connecting Tech Pros Worldwide Forums | Help | Site Map

How to create element in namespace

FabrizioSW@gmail.com
Guest
 
Posts: n/a
#1: Apr 21 '06
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" + @"""/>";


Martin Honnen
Guest
 
Posts: n/a
#2: Apr 21 '06

re: How to create element in namespace




FabrizioSW@gmail.com wrote:
[color=blue]
> 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>[/color]

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/
FabrizioSW@gmail.com
Guest
 
Posts: n/a
#3: Apr 21 '06

re: How to create element in namespace



Martin Honnen wrote:[color=blue]
> FabrizioSW@gmail.com wrote:
>[color=green]
> > Hi all i've to create a xml doc like this
> >[/color][/color]
[color=blue]
> Let us know if you really need to use XmlDocument and still need help
> with that.[/color]

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

Martin Honnen
Guest
 
Posts: n/a
#4: Apr 21 '06

re: How to create element in namespace




FabrizioSW@gmail.com wrote:

[color=blue]
> 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...[/color]

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/
FabrizioSW@gmail.com
Guest
 
Posts: n/a
#5: Apr 21 '06

re: How to create element in namespace



Martin Honnen wrote:
[color=blue]
> 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);
>[/color]

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

Closed Thread