使用XML文件中的数据填充datagridview。编辑并保存回XML C#

时间:2016-11-16 05:09:49

标签: c# xml datagridview

这是我编写xml代码的代码。对于这个xml代码我正在组合两个表来获取我的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文件:

<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>

我的问题是我希望person_id始终是文件中人员的计数。例如,当我删除上面的xml中的T​​anner数据时。 XML应该看起来像这样

<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>

1 个答案:

答案 0 :(得分:2)

试试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();
            }

        }
    }
}