Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XML文件中的数据填充datagridview。编辑并保存回XML C#_C#_Xml_Datagridview - Fatal编程技术网

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

使用XML文件中的数据填充datagridview。编辑并保存回XML C#,c#,xml,datagridview,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) {

下面是我编写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>
}
使用(XmlWriter=XmlWriter.Create(路径,xmlWriterSettings))
{
xmlWriter.WriteStarteElement(“地址”);
xmlWriter.WriteStarteElement(“工作”);
foreach(数据行中的数据行)
{
writeStart元素(“MD”);
foreach(数据列中的数据列)
{
xmlWriter.WriteElementString(dataColumn.ColumnName,dataRow[dataColumn].ToString());
}
xmlWriter.WriteEndElement();
}
xmlWriter.WriteEndElement()//
xmlWriter.WriteStarteElement(“人员”);
foreach(dt2.Rows中的DataRow-DataRow)
{
xmlWriter.WriteStarteElement(“人”);
foreach(dt2.Columns中的DataColumn-DataColumn)
{
xmlWriter.WriteElementString(dataColumn.ColumnName,dataRow[dataColumn].ToString());
}
xmlWriter.WriteEndElement()//
}
xmlWriter.WriteEndElement()//
xmlWriter.WriteEndElement()//
}
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>

1.
弗吉尼亚州
1.
玛拉
1.
0
2.
制革工
1.
0
3.
卡洛
1.
0
4.
凯西
1.
0
我的问题是我希望person_id始终是文件中的人数。例如,当我在上面的xml中删除Tanner的数据时。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.
弗吉尼亚州
1.
玛拉
1.
0
2.
卡洛
1.
0
3.
凯西
1.
0
试试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();
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序27
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
List person_ID=doc.subjections(“person_ID”).ToList();
整数计数=1;
foreach(个人ID中的XElement个人ID)
{
person_ID.Value=(count++).ToString();
}
}
}
}
试试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();
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序27
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
List person_ID=doc.subjections(“person_ID”).ToList();
整数计数=1;
foreach(个人ID中的XElement个人ID)
{
person_ID.Value=(count++).ToString();
}
}
}
}

到目前为止,您尝试过什么吗?如果是,请提供代码。如果没有,请尝试一下,并让我们知道问题。到目前为止,您尝试过什么吗?如果是,请提供代码。如果没有,请尝试一下,让我们知道问题所在。