chetan Allipur

chetan Allipur

  • NA
  • 541
  • 168.9k

How to get both Elements and Attributes to create XML File

Mar 20 2017 8:07 AM
Here is my code. Here i am getting only Elements. I want Attribute value also. Please anyone Help me.
 
private void Button_Click_1(object sender, RoutedEventArgs e)
{
try
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
XDocument Xdoc = new XDocument(new XElement("Users"));
if (System.IO.File.Exists(@"D:\Example.xml"))
Xdoc = XDocument.Load(@"D:\Example.xml");
else
{
Xdoc = new XDocument();
XElement xmlstart = new XElement("Users");
Xdoc.Add(xmlstart);
}
XElement xml = /*new XElement("Users",*/
new XElement("Person",
new XElement("Name", txtNames.Text)),
new XElement("Age", txtAge.Text),
new XElement("Address", txtAddress.Text));
if (Xdoc.Descendants().Count() > 0)
Xdoc.Descendants().First().Add(xml);
else
Xdoc.Add(xml);
Xdoc.Save(@"D:\Example.xml");
MessageBox.Show("Saved to XML File");
txtNames.Text = "";
txtAge.Text = "";
txtAddress.Text = "";
}
catch (Exception)
{
throw;
}
}
 

Answers (1)