How to create XML file with dynamic data using C#?

There is a sample code to create XML file with dynamic data. Code has been developed in C# whereas application is windows based. Scenario is used for businessrule but you can use this for any other approaches also.

How to create XML file with dynamic data using C#?
Suppose user wants to create a XML file which has predecided format but user can set attributes at runtime then this code snippet is used.
In this, user can create businessrule alongwith the description and user will give input values for setting those attributes.
XML file will be created as per the dynamic data.
config file is used to set the filepath where we want to store the newly created xml file.
You can create business rules or conditional cases by using this code.

XmlDocument my_XML_doc = new XmlDocument();
// Create root node.
XmlElement XElemRoot = my_XML_doc.CreateElement("BusinessRule");
XElemRoot.SetAttribute("BusinessRuleName", txtBusinessRule.Text);
//Add the node to the document.
XmlElement Xsource = my_XML_doc.CreateElement("BusinessRuleDescription");
XElemRoot.SetAttribute("BusinessRuleDescription", txtBusinessRuledescription.Text);

if (!Directory.Exists(ConfigurationSettings.AppSettings["user_file_path"] + txtBusinessRule.Text))
System.IO.Directory.CreateDirectory(ConfigurationSettings.AppSettings["user_file_path"] + txtBusinessRule.Text);
my_XML_doc.Save(ConfigurationSettings.AppSettings["user_file_path"] + txtBusinessRule.Text + "\\" + txtBusinessRule.Text + ".xml");
MessageBox.Show("BusinessRule Created", "BusinessRule Creation");
MessageBox.Show("Duplicate BusinessRule!", "BusinessRule creation error");
txtBusinessRule.Text = "";
txtBusinessRuledescription.Text = "";


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: