简体   繁体   English

如何将列表添加到 xml 值

[英]how to add a list to a xml value

In my C# console app I am trying to add a string to an xmlArray which I declared like this:在我的 C# 控制台应用程序中,我试图将一个字符串添加到我这样声明的 xmlArray 中:

        [XmlArray]
        public string[] carModeName = { };

In my xml it looks like this:在我的 xml 中,它看起来像这样:

<?xml version="1.0"?>
<ArrayOfAutokategorie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Autokategorie id="10">
    <name>Test</name>
    <feeByDay>45</feeByDay>
    <feeByWeek>45</feeByWeek>
    <carModeName />
  </Autokategorie>
</ArrayOfAutokategorie>

I want to try and add with my Method each time one condition is true my string to this array, I was trying to do it like this:每当一个条件为真时,我想尝试使用我的方法将我的字符串添加到这个数组中,我试图这样做:

public static void carAdderAfterAddingCar(int categoryId, int carId, string carCategoryName)
        {
            string filepath = serializerHelper.filePath = "carCategoryDb.xml";

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(filepath);
            serializerHelper sHelper = new serializerHelper();
            string[] carArray = { };
            List<Auto> carLists = sHelper.showData(typeof(List<Auto>), serializerHelper.filePath = "carDb.xml") as List<Auto>;
                string temp;

            foreach (var item in carLists)
            {


                xDoc.Load("carDb.xml");
                var tgt = xDoc.SelectSingleNode("ArrayOfAuto");
                var setToDelete = tgt.SelectSingleNode("Auto[@id=" + carId + "]"); //Filterung wie in den anderen klassen

                if (item.id.Equals(carId))
                {
                    temp = item.model; // Wenn die if erfüllt ist wird item.model also der model name der innerhalb unserer carList drin ist in eine neue liste eingepackt.
                    XDocument xDoc2 = XDocument.Load(filepath);
                    var tgt2 = xDoc2.Root.Descendants("Autokategorie").Where(x =>
                                          x.Attribute("id").Value.Equals(categoryId));

                    var carList = tgt2.Descendants("carModeName").FirstOrDefault();
                    Console.WriteLine("Bitte geben sie die neue gebühr/Tage ein");
                    carList.Add(temp);
                    xDoc.Save(filepath);
                    Console.Clear();
                    Console.WriteLine("\r\n " +carList.Value + " wurde der Kategorie: " + carCategoryName + " hinzugefügt");


                }

            }
            
 

But I am getting the error that carList is null, but I could not find why但我收到错误,carList 是 null,但我找不到原因

You can also try this你也可以试试这个

[XmlArray(ElementName = "carModeName")]
public List<string> CarModeName { get; set; }

or you can use the full property syntax as well.或者您也可以使用完整的属性语法。

use following for deserializing:使用以下反序列化:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfAutokategorie));
            ArrayOfAutokategorie arrayOfAutokategorie = (ArrayOfAutokategorie)serializer.Deserialize(reader); 
        }
    }
    public class ArrayOfAutokategorie
    {
        [XmlElement]
        public List<Autokategorie> Autokategorie { get; set; }
    }
    public class Autokategorie
    {
        [XmlAttribute]
        public int id { get; set; }
        public string name { get;set;}
        public int feeByDay { get; set; }
        public int feeByWeek { get; set; }
        public string carModeName { get; set; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM