【发布时间】: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 为空的错误,但我找不到原因
【问题讨论】: