Generate an XML document C# with Example



Generate an XML document C# with Example

The goal is to generate the following XML document: 
 
 
Banana 
Yellow 
 
 
Apple 
Red 
 
 
Code: 
XNamespace xns = "http://www.fruitauthority.fake"; 
XDeclaration xDeclaration = new XDeclaration("1.0", "utf-8", "yes"); 
XDocument xDoc = new XDocument(xDeclaration); 
XElement xRoot = new XElement(xns + "FruitBasket"); 
xDoc.Add(xRoot); 
XElement xelFruit1 = new XElement(xns + "Fruit"); 
XAttribute idAttribute1 = new XAttribute("ID", "F0001"); 
xelFruit1.Add(idAttribute1); 
XElement xelFruitName1 = new XElement(xns + "FruitName", "Banana"); 
XElement xelFruitColor1 = new XElement(xns + "FruitColor", "Yellow"); 
xelFruit1.Add(xelFruitName1); 
xelFruit1.Add(xelFruitColor1); 
xRoot.Add(xelFruit1); 
XElement xelFruit2 = new XElement(xns + "Fruit"); 
XAttribute idAttribute2 = new XAttribute("ID", "F0002"); 
xelFruit2.Add(idAttribute2); 
XElement xelFruitName2 = new XElement(xns + "FruitName", "Apple"); 
XElement xelFruitColor2 = new XElement(xns + "FruitColor", "Red"); 
xelFruit2.Add(xelFruitName2); 
xelFruit2.Add(xelFruitColor2); 
xRoot.Add(xelFruit2); 

0 Comment's

Comment Form

Submit Comment