Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
And also the code view in Dataset Source, create xml file using c# and send the data to dataset

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

<Company>

<Employees>

  <Employee Name="Mahesh Chand" Age="30" Phone="6101233333" /> 

  <Employee Name="Rose Garner" Age="56" Phone="2133428778" /> 

  <Employee Name="Amger Jap" Age="22" Phone="9092349800" /> 

  <Employee Name="Mike Gold" Age="35" Phone="9908088823" /> 

  <Employee Name="Renee Flower" Age="19" Phone="4848901003" /> 

</Employees>

</Company>

I have tried this code but its not working ( the xml not like the top one)
C#
XmlDocument doc = new XmlDocument();

//(1) the xml declaration is recommended, but not mandatory
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);

//(2) string.Empty makes cleaner code
XmlElement element1 = doc.CreateElement(string.Empty, "body", string.Empty);
doc.AppendChild(element1);

XmlElement element2 = doc.CreateElement(string.Empty, "level1", string.Empty);
element1.AppendChild(element2);

XmlElement element3 = doc.CreateElement(string.Empty, "level2", string.Empty);
XmlText text1 = doc.CreateTextNode("text = "+"");
element3.AppendChild(text1);
element2.AppendChild(element3);

XmlElement element4 = doc.CreateElement(string.Empty, "level2", string.Empty);
XmlText text2 = doc.CreateTextNode("other text");
element4.AppendChild(text2);
element2.AppendChild(element4);

doc.Save("ChartReport.xml");

DataSet ds = new DataSet();
ds.ReadXml("ChartReport.xml");
ds.WriteXmlSchema("DataSet1.xsd");

this.reportViewer1.RefreshReport();
Posted
Updated 21-Dec-14 0:42am
v6
Comments
DamithSL 21-Dec-14 5:48am    
you need to get xml from which data? update the question with your code
elmutasim23 21-Dec-14 5:57am    
I want only to create xml using c# and then send the data to dataset
Kornfeld Eliyahu Peter 21-Dec-14 5:58am    
http://mattgemmell.com/what-have-you-tried/

1) XML is just text, so you have the possibility to build it with StringBuilder[^] if you like

2) You can build XML as you tried, but of course, you need to adapt the code. Copy-pasting like a full won't help. See LinqPad[^] sample:
C#
private XmlNode NewEmployee(XmlDocument doc, string name, int age, string phone)
{
  var e = doc.CreateElement("Employee");
  var aA = doc.CreateAttribute("Age");
  aA.Value = age.ToString();
  
  var aN = doc.CreateAttribute("Name");
  aN.Value = name;
  
  var aP = doc.CreateAttribute("Phone");
  aP.Value = phone;
  
  e.Attributes.Append(aN);
  e.Attributes.Append(aA);
  e.Attributes.Append(aP);
  
  return e;
}

void Main()
{
	var doc = new XmlDocument();
	var root = doc.CreateElement("Company");
	doc.AppendChild(root);
	var es = doc.CreateElement("Employees");
	root.AppendChild(es);
	
	es.AppendChild(NewEmployee(doc, "Mahesh Chand", 30, "6101233333"));
	es.AppendChild(NewEmployee(doc, "Rose Garner",56, "2133428778"));
	
	doc.DumpFormatted();
}

3) And of couse you can use serialization, and strongly typed classes. See following example:
C#
[XmlRoot("Company")]
public class Company
{
	[XmlArrayItem("Employee", typeof(Employee))]
    [XmlArray("Employees")]
	public List<Employee> Employees;
}

public class Employee
{
 [XmlAttribute("Name")]
 public string Name {get; set;}
 [XmlAttribute("Age")]
 public int Age {get; set;}
 [XmlAttribute("Phone")]
 public string Phone {get;set;}
}

void Main()
{
	var c = new Company();
	c.Employees = new List<Employee>() 
		{
			new Employee() {Name = "Mahesh Chand", Age=30, Phone = "6101233333"},
			new Employee() {Name = "Rose Garner", Age=56, Phone="2133428778"}
		};
	
	
	XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
	ns.Add(string.Empty, string.Empty);
	TextWriter w = new StringWriter();
    var s = new XmlSerializer(typeof(Company));
    s.Serialize(w, c, ns);
    w.Flush();
	
	
	w.ToString().Dump();
}
 
Share this answer
 
v3
Comments
George Jonsson 21-Dec-14 7:24am    
I would not recommend using StringBuilder as it is very easy to make mistakes and create a badly formatted XML file.
Zoltán Zörgő 21-Dec-14 9:36am    
I haven't recommended it either. But it is an option, and in simple cases might be enough.
Maciej Los 21-Dec-14 16:23pm    
Serialization... This is it!
+5!
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search using part of your question as the search term gave over 2,000,000 hits: Google "Create xml file using c#"[^]
The second hit is MSDN guiding you through the process: MSDN: "Creating an XML File"[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 
Comments
elmutasim23 21-Dec-14 6:10am    
I tried but most of the xml using C# is not the same as the top that I post
It is not exactly clear what it is you actually want to do, because your code is faraway from the presented XML data.

If you want to create an XML file with the same structure as you have presented, you need to add several XmlAttribute[^] to your XmlElement.

In the code below I have created the first element. Adding more is just a matter of repetition.
C#
XmlDocument doc = new XmlDocument();

//(1) the xml declaration is recommended, but not mandatory
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmlDeclaration, root);

XmlElement elementRoot = doc.CreateElement(string.Empty, "Company", string.Empty);
doc.AppendChild(elementRoot);

//(2) string.Empty makes cleaner code
XmlElement element1 = doc.CreateElement(string.Empty, "Employees", string.Empty);
elementRoot.AppendChild(element1);

XmlElement element2 = doc.CreateElement(string.Empty, "Employee", string.Empty);

// Create and add Name attribute
XmlAttribute attrib1 = doc.CreateAttribute("Name");
attrib1.Value = "Mahesh Chand";
element2.Attributes.Append(attrib1);

// Create and add Age attribute
XmlAttribute attrib2 = doc.CreateAttribute("Age");
attrib2.Value = "30";
element2.Attributes.Append(attrib2);

// Create and add Phone attribute
XmlAttribute attrib3 = doc.CreateAttribute("Phone");
attrib3.Value = "6101233333";
element2.Attributes.Append(attrib3);

element1.AppendChild(element2);

// Add more elements with attributes
// ...

doc.Save("ChartReport.xml");


That said and done, the question arise why you want to first create an XML file and then load it into the dataset.
 
Share this answer
 
Comments
elmutasim23 21-Dec-14 14:17pm    
the xml is easy set and also the text can be changed in dataset but the datatable write much code and can't be changed easily

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900