尝试将表单数据保存回XML文件

时间:2014-04-22 22:05:49

标签: c# xml

我有一个表单,用户可以从我包含的XML文件中检索信息。但是现在我想把它转到用户更改和信息的位置,然后点击保存按钮,数据将保存到XML文件。我似乎无法做到这一点。我也有一个SaveVendors方法,我想知道如何调用该方法。我已经包含了我尝试过的代码,方法和XML文件。感谢。

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>

1 个答案:

答案 0 :(得分:1)

首先,您应该使用XDocument。 (using System.Xml.Linq

以下内容可以帮助您。

<强> 1。加载XML文档:

var xDoc = XDocument.Load(xmlPath);

<强> 2。创建新元素的代码:

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。如果您纯粹在文档末尾添加新的<Vendor>元素:

xDoc.Root.Add(newEle);

<强> 3B。假设您想编辑名为“Allegheny Drink Products”的供应商,您可以这样写:

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

<强> 4。现在保存文档:

xDoc.Save(xmlPath);