tags:

views:

362

answers:

4

Sorry to ask such a silly question, but I got stuck up ..........

I need to create a xml file dynamically, the structure of that file is-->

<Login>
  <id userName="Tushar" passWord="Tushar">
      <Name>Tushar</Name>
      <Age>24</Age>
  </id>
</Login>

I am not able to create the attributes inside the root tag (ie userName="" and passWord="") is not being created

I am using simple windows application

Some Important namespace that you might require is

using System.Xml;
using System.IO;
+7  A: 

Well id isn't really the root node: Login is.

It should just be a case of specifying the attributes (not tags, btw) using XmlElement.SetAttribute. You haven't specified how you're creating the file though - whether you're using XmlWriter, the DOM, or any other XML API.

If you could give an example of the code you've got which isn't working, that would help a lot. In the meantime, here's some code which creates the file you described:

using System;
using System.Xml;

class Test
{
    static void Main()
    {
        XmlDocument doc = new XmlDocument();
        XmlElement root = doc.CreateElement("Login");
        XmlElement id = doc.CreateElement("id");
        id.SetAttribute("userName", "Tushar");
        id.SetAttribute("passWord", "Tushar");
        XmlElement name = doc.CreateElement("Name");
        name.InnerText = "Tushar";
        XmlElement age = doc.CreateElement("Age");
        age.InnerText = "24";

        id.AppendChild(name);
        id.AppendChild(age);
        root.AppendChild(id);
        doc.AppendChild(root);

        doc.Save("test.xml");
    }
}
Jon Skeet
A: 

Your question is already aswnered but if you just want to create an xml file and not modify it at run-time, you can create it in String format and then simply save it.

Nick D
You should try to never manipulate XML as as string. The rules are different. Try "<name>" + name + "</name>" with name == "O'Brien".
John Saunders
I didn't say manipulate XML as a string. I said create and save.And I won't bite your little trick :)Handling strings manually is risky. I know. But even with an XML parser we aren't *safe* and we should always validate our data.
Nick D
A: 

For might also want to consider XML serialization.

kenny
+5  A: 

The latest and supposedly greatest way to construct the XML is by using LINQ to XML:

using System.Xml.Linq

       var xmlNode =
            new XElement("Login",
                         new XElement("id",
                             new XAttribute("userName", "Tushar"),
                             new XAttribute("password", "Tushar"),
                             new XElement("Name", "Tushar"),
                             new XElement("Age", "24")
                         )
            );
       xmlNode.Save("Tushar.xml");

Supposedly this way of coding should be easier, as the code closely resembles the output (which Jon's example above does not). However, I found that while coding this relatively easy example I was prone to lose my way between the cartload of comma's that you have to navigate among. Visual studio's auto spacing of code doest not help either.

Dabblernl
+1 for the new hotness
Dan Blair