-2

I'm sorry, I'm not good with XML APIs. How would I generate an XML file of the following format and write it?

<?xml version="1.0" encoding="utf-8" ?> 

<ROOT>
  <LOC ID="*">
    <ROW ID = "1" CD = "US" DESC = "United States" ISACTIVE="1" ORDER="1"/>
    <ROW ID = "2" CD = "CA" DESC = "Canada" ISACTIVE="1" ORDER="2"/>
    <ROW ID = "3" CD = "XX" DESC = "Others" ISACTIVE="1" ORDER="3"/>
  </LOC>
</ROOT>

Here's my best first attempt. The hardcoded values would have to be replaced by values from the database. I don't know how to iterate and create the multiple row elements.

XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("ROOT");
xmlDoc.AppendChild(rootNode);

XmlNode locNode = xmlDoc.CreateElement("LOC");
XmlAttribute attr = xmlDoc.CreateAttribute("ID");
attr.Value = "*";
rootNode.AppendChild(locNode);

XmlNode rowNode = xmlDoc.CreateElement("ROW");
XmlAttribute id = xmlDoc.CreateAttribute("CD");
id.Value = "1";
XmlAttribute cd = xmlDoc.CreateAttribute("CD");
cd.Value = "US";
XmlAttribute desc = xmlDoc.CreateAttribute("DESC");
desc.Value = "United States";
XmlAttribute active = xmlDoc.CreateAttribute("ISACTIVE");
active.Value = "1";
XmlAttribute order = xmlDoc.CreateAttribute("ORDER");
order.Value = "1";
rootNode.AppendChild(rowNode);

xmlDoc.Save("foo.xml");
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
user2471435
  • 1,644
  • 7
  • 35
  • 62
  • Let's see how far you have gotten so far? – musefan Oct 27 '14 at 16:37
  • Post your best attempt and let us know what the problems are. – nvoigt Oct 27 '14 at 16:37
  • possible duplicate of [What is the best way to build XML in C# code?](http://stackoverflow.com/questions/284324/what-is-the-best-way-to-build-xml-in-c-sharp-code) – Alex K. Oct 27 '14 at 16:56
  • There's an enormous amount of documentation and examples around building/reading XML if you'd just bother to look for it. Go do some research and try it yourself and come back when you run into a problem with *code you've written*. Without any actual effort shown, it sounds like you're just asking us to do it for you. – tnw Oct 27 '14 at 17:06
  • I understand and I am coding my best attempt as we speak. – user2471435 Oct 27 '14 at 17:23
  • I've updated the above to show my best attempt. – user2471435 Oct 27 '14 at 17:36
  • You would "iterate and create" the multi row elements by using a loop. If it's coming from a database, it would probably need to be a `while` that would terminate when you reach the end of the data. If it's a finite number, a `for` loop would work. There's nothing complicated about the loop itself. For each element you need to add, you create the element, add the attributes and set values just as you are and append them to `locNode` (not the `rootNode`). – Ken White Oct 27 '14 at 17:48
  • How about the XML? Is it anywhere close? I don't know the APIs but I made my best attempt – user2471435 Oct 27 '14 at 17:50

1 Answers1

1

Easier using System.Xml.Linq;

    ...
    var xml = new XElement("ROOT", 
        new XElement("LOC", new XAttribute("ID", "*"),
            CreateRow(1, "US", "United States", 1, 1),
            CreateRow(2, "CA", "Canada", 1, 2),
            CreateRow(3, "XX", "UOthers", 1, 3)));

    Console.WriteLine(xml);
}

static XElement CreateRow(int id, string cd, string desc, int isActive, int order)
{
    return new XElement("ROW",
        new XAttribute("ID", id),
        new XAttribute("CD", cd),
        new XAttribute("DESC", desc),
        new XAttribute("ISACTIVE", isActive),
        new XAttribute("ORDER", order));
}

Loop;

var xml = new XElement("LOC", new XAttribute("ID", "*"));

for (var i = 1; i < 10; i++)
{
    xml.Add(CreateRow(i, "?", "?", 1, i));
}

Console.WriteLine(new XElement("ROOT", xml));
Alex K.
  • 171,639
  • 30
  • 264
  • 288