如何使用C#将数据附加/更新到XML文档的各个组和字段中

问题描述 投票:0回答:1

下面是我的XML文件在记事本上的样子,我想在后面将数据添加到字段62-66中。谁能帮我用C#或DOTNET做到这一点谢谢..

<?xml version="1.0" encoding="UTF-8"?>
<?mso-infoPathSolution productVersion="14.0.0" PIVersion="1.0.0.0" href="http://www.website.com/sites/infopathforms/SOPMAT01709%20V30/Forms/template.xsn" name="urn:schemas-microsoft-com:office:infopath:SOP-MAT-01709-F01-V3-0:-myXSD-2015-01-02T12-23-08" solutionVersion="1.0.0.429" ?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?>
<?mso-infoPath-file-attachment-present?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:st="urn:schemas-microsoft-com:office:smarttags" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2015-01-02T12:23:08" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
<my:group10>
    <my:group11>
        <my:field56></my:field56>
        <my:field181></my:field181>
        </my:group11>
</my:group10>
        <my:field62></my:field62>
        <my:field63></my:field63>
        <my:field64></my:field64>
        <my:field65></my:field65>
        <my:field66></my:field66>
        <my:group13/>
            <my:group15>
        <my:group16/>
            </my:group15>
c# .net
1个回答
0
投票

使用Xml Linq:

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

namespace ConsoleApplication139
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string[] changeRows = { "field62", "field63", "field64", "field65", "field66" };
            XDocument doc = XDocument.Load(FILENAME);

            foreach(XElement row in doc.Descendants().Where(x => changeRows.Contains(x.Name.LocalName )))
            {
                row.SetValue("1");
            }

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.