Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / April 2006

Tip: Looking for answers? Try searching our database.

How to create element in namespace

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
FabrizioSW@gmail.com - 21 Apr 2006 11:07 GMT
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","http://www.w3.org/2001/XInclude");
doc.AppendChild(main);

main.InnerXml=@"<xi:include href=""" + "one.xml" + @"""/>";
Martin Honnen - 21 Apr 2006 13:42 GMT
> Hi all i've to create a xml doc like this
>
[quoted text clipped - 4 lines]
> <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.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

FabrizioSW@gmail.com - 21 Apr 2006 16:08 GMT
> > 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
Martin Honnen - 21 Apr 2006 16:38 GMT
> 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(attribute);

    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);

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

FabrizioSW@gmail.com - 21 Apr 2006 18:55 GMT
> 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
[quoted text clipped - 51 lines]
>      // now save somewhere e.g.
>      xmlDocument.Save(Console.Out);

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.