卡在编辑XML文件上

时间:2019-12-07 11:51:05

标签: c# xml nodes

第一个帖子在这里..:D 因此,我被困在编辑XML文件和添加新节点的过程中。 这是我的XML的样子:

    <?xml version="1.0" encoding="utf-8"?>
    <Racuni>
    <------ I want to make new <racun> here with new data imported from list or at the end 
      <Racun>
        <datumkreiranjaracuna>20191230</datumkreiranjaracuna>
        <nazivulja>suncokretovo</nazivulja>
        <kolicinaulja>50</kolicinaulja>
        <cijenaulja>25</cijenaulja>
      </Racun>
    <------- or here :D
    </Racuni>

每次打开控制台应用程序时,我想在Racun之间使用列表中的新数据添加新的Racuni。 (无论是在第一个Racun之前还是在最后一个Racun之后,我都希望将其保存在那里。

谢谢!

3 个答案:

答案 0 :(得分:2)

也许您应该使用XmlDocument,因为它可以更轻松地处理您所描述的任务:

var xdoc = new XmlDocument();
    xdoc.LoadXml(@"
    <?xml version='1.0' encoding='utf-8'?>
    <Racuni>
    <!-- I want to make new <racun> here with new data imported from list or at the end -->
      <Racun>
        <datumkreiranjaracuna>20191230</datumkreiranjaracuna>
        <nazivulja>suncokretovo</nazivulja>
        <kolicinaulja>50</kolicinaulja>
        <cijenaulja>25</cijenaulja>
      </Racun>
    <!-- or here :D -->
    </Racuni>");

// you must use the original XmlDocument to create new elements
var racun = xdoc.CreateElement("Racun");
var dat = xdoc.CreateElement("datumkreiranjaracuna");
    dat.InnerText = "20191230";
var naz = xdoc.CreateElement("nazivulja");
    naz.InnerText = "suncokretovo";
var kol = xdoc.CreateElement("kolicinaulja");
    naz.InnerText = "50";
var cij = xdoc.CreateElement("cijenaulja");
    naz.InnerText = "25";

// now, add the created nodes into the proper places
racun.AppendChild(dat);
racun.AppendChild(naz);
racun.AppendChild(kol);
racun.AppendChild(cij);

// finally, add the new element back into the xml document
//xdoc.DocumentElement.AppendChild(racun);
xdoc.SelectSingleNode("/Racuni").AppendChild(racun);

// display the results
xdoc.Save(Console.Out);

您还应该查看类似CreateAttribute的方法,并且还应该知道使用XPath来定位特定元素。

答案 1 :(得分:1)

欢迎使用Stackoverflow。

有几种方法可以从c#中编辑XML。

XML序列化器

如果具有相应的模型,则可以使用XMLSerializer对XML文档进行反序列化,然后使用生成的模型对数据进行更改,然后再次对模型进行序列化。 https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer?view=netframework-4.8

XPath

XPath是一种导航类型,如果您熟悉的话,它与JQuery是“相似的”。它使您可以加载xml文档,然后导航到其中并进行更改。

https://docs.microsoft.com/en-us/dotnet/standard/data/xml/select-nodes-using-xpath-navigation

XmlDocument

XmlDocument也是XPath的基础,但不仅限于此。您可以使用其方法浏览和操作XML文档,然后再次保存

https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmldocument?view=netframework-4.8

答案 2 :(得分: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)
        {
            List<Racun> racuni = new List<Racun>() {
                new Racun() { 
                    datumkreiranjaracuna = DateTime.Now,
                    nazivulja = "suncokretovo",
                    kolicinaulja = 50,
                    cijenaulja = 25
                },
                new Racun() { 
                    datumkreiranjaracuna = DateTime.Now.AddDays(1),
                    nazivulja = "suncokretovo",
                    kolicinaulja = 60,
                    cijenaulja = 30
                },
                new Racun() { 
                    datumkreiranjaracuna = DateTime.Now.AddDays(2),
                    nazivulja = "suncokretovo",
                    kolicinaulja = 70,
                    cijenaulja = 35
                }
            };

            string xmlIdent = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Racuni></Racuni>";

            XDocument doc = XDocument.Parse(xmlIdent);
            XElement xRacuni = doc.Root;

            foreach (Racun racun in racuni)
            {
                XElement newRacun = new XElement("Racun", new object[] {
                    new XElement("datumkreiranjaracuna", racun.datumkreiranjaracuna.ToString("yyyyMMdd")),
                    new XElement("nazivulja", racun.nazivulja),
                    new XElement("kolicinaulja", racun.kolicinaulja),
                    new XElement("cijenaulja", racun.cijenaulja)
                });
                xRacuni.Add(newRacun);
            }

            doc.Save(FILENAME);
        }
    }
    public class Racun
    {
        public DateTime datumkreiranjaracuna { get; set; }
        public string nazivulja { get; set; }
        public int kolicinaulja { get; set; }
        public int cijenaulja { get; set; }
    }
}

将代码添加到现有文件中

            XDocument doc = XDocument.Load(FILENAME);
            XElement xRacuni = doc.Root;

            foreach (Racun racun in racuni)
            {
                XElement newRacun = new XElement("Racun", new object[] {
                    new XElement("datumkreiranjaracuna", racun.datumkreiranjaracuna.ToString("yyyyMMdd")),
                    new XElement("nazivulja", racun.nazivulja),
                    new XElement("kolicinaulja", racun.kolicinaulja),
                    new XElement("cijenaulja", racun.cijenaulja)
                });
                xRacuni.Add(newRacun);
            }

            doc.Save(FILENAME);