【问题标题】:how to add a list to a xml value如何将列表添加到 xml 值
【发布时间】:2021-06-18 09:13:29
【问题描述】:

在我的 C# 控制台应用程序中,我试图将一个字符串添加到我这样声明的 xmlArray:

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

在我的 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>

每当一个条件为真时,我想尝试使用我的方法将我的字符串添加到这个数组中,我试图这样做:

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");


                }

            }
            
 

但我收到 carList 为空的错误,但我找不到原因

【问题讨论】:

    标签: c# .net xml


    【解决方案1】:

    你也可以试试这个

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

    或者您也可以使用完整的属性语法。

    【讨论】:

      【解决方案2】:

      使用以下反序列化:

      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; }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多