user3552805
user3552805

Reputation: 19

Trying to save form data to back to an XML file

I have a form where a user is able to retrieve information from an XML file that I included. But now I want to make it to where if the user changes and information and hits the save button that the data will save to the XML file. I just can't seem to get it right. I also have a SaveVendors method and I was wondering as to how to call that method. I have included the code I tried, the method and the XML file. Thanks.

private void btnSave_Click(object sender, EventArgs e)
        {




           if (IsValidData()) //8 constructors and how will it connect to XML file
            {
                vendors = new Vendor(txtName, txtAddress, txtCity, txtPhone);

                this.Close();
            }
            //run validation, then take data and save it to XML file.



       }

  public static void SaveVendors(List<Vendor> vendors)
        {
            // create the XmlWriterSettings object
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");

            // create the XmlWriter object
            XmlWriter xmlOut = XmlWriter.Create(Path, settings);

            // write the start of the document
            xmlOut.WriteStartDocument();
            xmlOut.WriteStartElement("Vendors");

            // write each product object to the xml file
            foreach (Vendor vendor in vendors)
            {
                xmlOut.WriteStartElement("Vendor");
                xmlOut.WriteElementString("Name", vendor.Name);
                xmlOut.WriteElementString("Address", vendor.Address);
                xmlOut.WriteElementString("City", vendor.City);
                xmlOut.WriteElementString("State", vendor.State);
                xmlOut.WriteElementString("ZIP", vendor.Zip);
                xmlOut.WriteElementString("Phone", vendor.Phone);
                xmlOut.WriteElementString("YTD", Convert.ToString(vendor.YTD));
                xmlOut.WriteElementString("Comment", vendor.Comment);
                xmlOut.WriteElementString("Contact", vendor.Contact);
                xmlOut.WriteElementString("DefaultDiscount", Convert.ToString(vendor.DefaultDiscount));

                xmlOut.WriteEndElement();
            }

<?xml version="1.0" encoding="utf-8"?>
<Vendors>
  <Vendor>
    <Name>Scarmado Produce Co.</Name>
    <Address>244 Southwest Dr.</Address>
    <City>Bryan</City>
    <State>Texas</State>
    <ZIP>77805</ZIP>
    <Phone>9797784456</Phone>
    <YTD>2500000</YTD>
    <Comment>Great vendor</Comment>
    <Contact>James Scarmado</Contact>
    <DefaultDiscount>15</DefaultDiscount>
  </Vendor>
  <Vendor>
    <Name>Talbort Restaurant Supplies</Name>
    <Address>2533 Broadway Ave.</Address>
    <City>New Orleans</City>
    <State>LA USA</State>
    <ZIP>89554</ZIP>
    <Phone>7664028762</Phone>
    <YTD>1589000</YTD>
    <Comment></Comment>
    <Contact>Joan Fishing</Contact>
    <DefaultDiscount>20</DefaultDiscount>
  </Vendor>
  <Vendor>
    <Name>Famous Meats</Name>
    <Address>222 Swey Ave.</Address>
    <City>Bangkok</City>
    <State>Thailand</State>
    <ZIP>75110</ZIP>
    <Phone>883778723</Phone>
    <YTD>186000</YTD>
    <Comment>Good vendor</Comment>
    <Contact>Faye Smith</Contact>
    <DefaultDiscount>15</DefaultDiscount>
  </Vendor>
  <Vendor>
    <Name>Duetsch Products</Name>
    <Address>253 Hamburg Ave.</Address>
    <City>Hamburg</City>
    <State>Germany</State>
    <ZIP>APO 76632</ZIP>
    <Phone>109019834</Phone>
    <YTD>258260</YTD>
    <Comment>Difficult delivery system</Comment>
    <Contact>Ian Friberg</Contact>
    <DefaultDiscount>20</DefaultDiscount>
  </Vendor>
  <Vendor>
    <Name>Allegheny Drink Products</Name>
    <Address>2862 Hamilton St.</Address>
    <City>Dallas</City>
    <State>Texas</State>
    <ZIP>80256</ZIP>
    <Phone>7149872957</Phone>
    <YTD>875390</YTD>
    <Comment></Comment>
    <Contact>Cheryl Montana</Contact>
    <DefaultDiscount>15</DefaultDiscount>
  </Vendor>
  <Vendor>
    <Name>Best Cheeses Around</Name>
    <Address>2732 Gouda Ct.</Address>
    <City>Lincoln</City>
    <State>Nebraska USA</State>
    <ZIP>67499</ZIP>
    <Phone>4028776509</Phone>
    <YTD>679270</YTD>
    <Comment></Comment>
    <Contact>Dave Pfister</Contact>
    <DefaultDiscount>15</DefaultDiscount>
  </Vendor>
</Vendors>

Upvotes: 0

Views: 106

Answers (1)

mnsr
mnsr

Reputation: 12437

Firstly, you should use XDocument. (using System.Xml.Linq)

The following should help you along your way.

1. Load your XML Document:

var xDoc = XDocument.Load(xmlPath);

2. The code to Create a new Element:

var newEle = new XElement("Vendor",
    new XElement("Name", vendor.Name),
    new XElement("Address", vendor.Address),
    new XElement("City", vendor.City),
    new XElement("State", vendor.State),
    new XElement("ZIP", vendor.Zip),
    new XElement("Phone", vendor.Phone),
    new XElement("YTD", Convert.ToString(vendor.YTD)),
    new XElement("Comment", vendor.Comment),
    new XElement("Contact", vendor.Contact),
    new XElement("DefaultDiscount", Convert.ToString(vendor.DefaultDiscount)));

3a. If you're purely adding a new <Vendor> element to the end of the document:

xDoc.Root.Add(newEle);

3b. Lets say you wanted to edit the Vendor called "Allegheny Drink Products", you'd write this:

xDoc.Descendants("Vendor")
    .Where(w => w.Element("Name").Value == "Allegheny Drink Products")
    .First()
    .ReplaceWith(newEle); // Replaces that element with newEle created above.

4. Now to save the document:

xDoc.Save(xmlPath);

Upvotes: 1

Related Questions