简体   繁体   中英

populating datagridview with data from XML file. edit and save back to XML C#

Here is my code to write my xml code. for this xml code i am combining two table to get the specific format for my xml

using (XmlWriter xmlWriter = XmlWriter.Create(path, xmlWriterSettings))
{
    xmlWriter.WriteStartElement("Address");
    xmlWriter.WriteStartElement("Work");
    foreach (DataRow dataRow in dt.Rows)
    {
        xmlWriter.WriteStartElement("MD");
        foreach (DataColumn dataColumn in dt.Columns)
        {
           xmlWriter.WriteElementString(dataColumn.ColumnName, dataRow[dataColumn].ToString());
        }
        xmlWriter.WriteEndElement();
    }
    xmlWriter.WriteEndElement();//</Work>
    xmlWriter.WriteStartElement("Persons");
    foreach (DataRow dataRow in dt2.Rows)
    {
        xmlWriter.WriteStartElement("Person");
        foreach (DataColumn dataColumn in dt2.Columns)
        {
             xmlWriter.WriteElementString(dataColumn.ColumnName, dataRow[dataColumn].ToString());
        }
        xmlWriter.WriteEndElement();//</Person>
    }
    xmlWriter.WriteEndElement();//</Persons>
    xmlWriter.WriteEndElement();//</Address>
}

XML file:

<Address>
  <Work>
     <Location>
        <Location_ID>1</Location_ID>
        <Location_NAME>Virginia</Location_NAME>
     </Location>
  </Work>
  <Persons>
     <Person>
        <Person_ID>1</Person_ID>
        <Person_Name>MARA</Person_Name>
        <Location_ID>1</Location_ID>
        <IsRequired>0</IsRequired>
     </Person>
     <Person>
        <Person_ID>2</Person_ID>
        <Person_Name>Tanner</Person_Name>
        <Location_ID>1</Location_ID>
        <IsRequired>0</IsRequired>
     </Person>
     <Person>
        <Person_ID>3</Person_ID>
        <Person_Name>Carlo</Person_Name>
        <Location_ID>1</Location_ID>
        <IsRequired>0</IsRequired>
     </Person>
     <Person>
        <Person_ID>4</Person_ID>
        <Person_Name>Casey</Person_Name>
        <Location_ID>1</Location_ID>
        <IsRequired>0</IsRequired>
     </Person>
  </Person>
<Address>

my problem is i want the person_id to always be a count of the persons on the file. for example when i delete data of Tanner in the above xml. the XML should look like this

<Address>
  <Work>
     <Location>
        <Location_ID>1</Location_ID>
        <Location_NAME>Virginia</Location_NAME>
     </Location>
  </Work>
  <Persons>
     <Person>
        <Person_ID>1</Person_ID>
        <Person_Name>MARA</Person_Name>
        <Location_ID>1</Location_ID>
        <IsRequired>0</IsRequired>
     </Person>
     <Person>
        <Person_ID>2</Person_ID>
        <Person_Name>Carlo</Person_Name>
        <Location_ID>1</Location_ID>
        <IsRequired>0</IsRequired>
     </Person>
     <Person>
        <Person_ID>3</Person_ID>
        <Person_Name>Casey</Person_Name>
        <Location_ID>1</Location_ID>
        <IsRequired>0</IsRequired>
     </Person>
  </Person>
<Address>

Try xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication27
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            List<XElement> person_IDs = doc.Descendants("Person_ID").ToList();
            int count = 1;
            foreach (XElement person_ID in person_IDs)
            {
                person_ID.Value = (count++).ToString();
            }

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM