0

I have an xml file which contains the following structure

   <Planet>
      <Continent name="Africa">
        <Country name="Algeria" />
        <Country name="Angola" />
          ...
      </Continent>
    </Planet>

I need to add to it the rest of the continent tags with there containing cities. This is my code:

       public static string continent;
       public static List<string> countries = new List<string>();

       XmlDocument xDoc = new XmlDocument();
       xDoc.Load(@"D:\Projects IDE\Visual Studio\Tutorial\e-commerce\classModeling\GenerateXml file\GenerateXml file\bin\Debug\Planet.xml");

        XmlNode xNode = xDoc.CreateNode(XmlNodeType.Element, "Continent", "");
        XmlAttribute xKey = xDoc.CreateAttribute("name");
        xKey.Value = continent;
        xNode.Attributes.Append(xKey);
        xDoc.GetElementsByTagName("Planet")[0].InsertAfter(xNode , xDoc.GetElementsByTagName("Planet")[0].LastChild);


        foreach (var country in countries)
        {
            XmlElement root = xDoc.CreateElement("Country");
            XmlAttribute xsKey = xDoc.CreateAttribute("name");
            xsKey.Value = country;
            root.Attributes.Append(xKey);

        }     
        xDoc.Save(@"D:\Projects IDE\Visual Studio\Tutorial\e-commerce\classModeling\GenerateXml file\GenerateXml file\bin\Debug\Planet.xml");    

My code creates all the tags but does not add the attributes.

And before anyone asks the continent variable and countries list contain the needed items I just felt it's not needed to display that part of the code two.

What am I doing wrong here?

EDIT

I managed to corect the code and now it work the attributes were not apearing because I gave both the the node attribute and element attribute the same name I changd the names , and now it works.

1
  • 1
    To tell what you are doing wrong, it would help to see what the results are when you run this code and how they are different from what you expect. Commented Nov 29, 2012 at 22:50

3 Answers 3

2

It is very easy to create xml with Linq to Xml:

XDocument xdoc = XDocument.Load(path_to_xml);
xdoc.Root.Add(
    new XElement("Continent", 
        new XAttribute("name", continent),
        from country in countries
        select new XElement("Country", new XAttribute("name", country))));
xdoc.Save(path_to_xml);

This code will add another <Continent> element (with provided countries) to Planet element. E.g. with following data

continent = "Europe";
countries = new List<string>() { "Spain", "France", "Italy", "Belarus" };

output will be

<Planet>
  <Continent name="Africa">
    <Country name="Algeria" />
    <Country name="Angola" />
  </Continent>
  <Continent name="Europe">
    <Country name="Spain" />
    <Country name="France" />
    <Country name="Italy" />
    <Country name="Belarus" />
  </Continent>
</Planet>
Sign up to request clarification or add additional context in comments.

Comments

1

well, in the following loop

    foreach (var country in countries)
    {
        XmlElement root = xDoc.CreateElement("Country");
        XmlAttribute xsKey = xDoc.CreateAttribute("name");
        xsKey.Value = continent;
        root.Attributes.Append(xKey);

    }   

you're creating your Country element, but then you don't do anything with it and root goes out of scope. were you meaning to add it to your Continent Tag?

maybe you want to add

xNode.AppendChild(root);

at the end of your loop

2 Comments

yes I was and while writing the question I realized that and fixed the problem but now I can't seem to get the attributes to appear I believe because they have the same name
@NistorAlexandru what do you mean you can't get the attributes to appear? do you have empty <Country> tags with no attributes in them?
0

You are adding the Tag Country but that method only gives you back the reference of the newly created element, then you have to add it explicitly to the document

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.