【问题标题】:Need to add new items under the existing XML file using C#需要使用 C# 在现有 XML 文件下添加新项目
【发布时间】:2021-06-14 13:41:38
【问题描述】:

我需要使用 C# 在现有文件中添加新项目,请提供最佳逻辑来使用 C#。 下面是我的 XML 文件(输入)=

<?xml version="1.0" encoding="UTF-8"?>
<FileSetting xmlns="http://ltsc.ieee.org/xsd/LOM" xmlns:xs="http://www.M1.org/2001/XMLSchema">
   <Files>
      <File Section="Section 1" Method="Complete" Location="Total 1">
         <Columns>
            <Profile Method="DataCollection" Item="All" />
         </Columns>
      </File>
      <!--  <File Section="Section 2" Method="Complete" Location="Total 2">
    <Columns>
      <Profile Method= "DataCollection" Item="All"/>
     </Columns>
  </File> -->
      <File Section="Section 3" Method="Complete" Location="Main">
         <Columns>
            <Profile Method="DataCollection" Item="All" />
         </Columns>
      </File>
   </Files>
</FileSetting>

在我现有的 XML 文件中,我想在 Files 下添加新的 File 项,所以 我期望输出为=

<?xml version="1.0" encoding="UTF-8"?>
<FileSetting xmlns="http://ltsc.ieee.org/xsd/LOM" xmlns:xs="http://www.M1.org/2001/XMLSchema">
   <Files>
      <File Section="Section 1" Method="Complete" Location="Total 1">
         <Columns>
            <Profile Method="DataCollection" Item="All" />
         </Columns>
      </File>
      <!--  <File Section="Section 2" Method="Complete" Location="Total 2">
    <Columns>
      <Profile Method= "DataCollection" Item="All"/>
     </Columns>
  </File> -->
      <File Section="Section 3" Method="Complete" Location="Main">
         <Columns>
            <Profile Method="DataCollection" Item="All" />
         </Columns>
      </File>
      <File Section="Section 4" Method="NotComplete" Location="Test5">
         <Columns>
            <Profile Method="DataCollecter" Item="Partial" />
         </Columns>
      </File>
   </Files>
</FileSetting>

谁能提供在 C# 中执行此操作的最佳逻辑?

提前致谢

【问题讨论】:

    标签: c# xml linq-to-xml xmldocument


    【解决方案1】:

    使用 xml linq:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load(FILENAME);
                XNamespace ns = doc.Root.GetDefaultNamespace();
    
                XElement files = doc.Descendants(ns + "Files").FirstOrDefault();
    
                XElement newFile = new XElement(ns + "File", new object[] {
                    new XAttribute("Section", "Section 4"),
                    new XAttribute("Method", "NotComplete"),
                    new XAttribute("Location", "Test5"),
                    new XElement(ns + "Columns", new object[] {
                        new XElement("Profile", new object[] {
                            new XAttribute("Method", "DataCollecter"),
                            new XAttribute("Item", "Partial")
                        })
                    })
                });
    
                files.Add(newFile);
            }
        }
    }
    

    【讨论】:

    • 非常感谢,它的工作。但我也得到 xmlns="" 文件名如下。 我没有在输出中需要这个,因为我们不会在输出中得到它?非常感谢您的帮助
    • 在我发布答案后,我认为这可能会发生。我在代码的两个位置添加了“ns +”。
    • 如果 XML 文件发生了变化,并且 XML 有一些额外的节点,所以它在输出中删除了这些节点,有什么方法可以在文件下添加项目并且它不应该影响如果 XML 也有其他节点,其他节点呢?提前感谢您的大力帮助
    • 上面的代码不应该删除任何节点。您在原始 xml 中有 cmets,可能看起来像正在删除节点,但这些节点是 cmets。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2014-06-20
    • 1970-01-01
    相关资源
    最近更新 更多