首页 > 解决方案 > XML 元素值作为字符串写入给出 IO 错误 C#

问题描述

我正在尝试保存一个日志 XML 文件。代码示例如下。

private class LogFunctionInfo
{
    private string logName;
    public string FunctionName;
    public Type FunctionClassType;
    public int Displayed;
    public int Offset;
    public int Multiplier;

    public string LogName { get => logName; set => logName = value; }
    public override string ToString()
    {
        return logName;
    }
}

private LogFunctionInfo[] LoggingFunctions = {bla bla bla};
loggingNames = LoggingFunctions.Select(a => a.ToString().Replace(" ", "").Clone().ToString()).ToArray();
do
{
    try
    {
        XElement xml = XElement.Load(xmlLogFileName);

        for (int LineCounter = 0; LineCounter < LogBufferCounter; LineCounter++)
        {
            Dictionary<string, int> dictionary = new Dictionary<string, int>();

            for (int count = 0; count < LogNumber; count++)
            {
                //dictionary.Add("Value" + count, LogBuffer[LineCounter][count]); // If I use this, its working like a charm.
                dictionary.Add(loggingNames[count], LogBuffer[LineCounter][count]); // If I use this, giving XmlException at System.Xml.dll error
                minute = count == 22 ? LogBuffer[LineCounter][count] : minute;
            }
            xml.Add(new XElement("GraphValues",
                new XAttribute("Time", minute),
                new XElement("Values",
                dictionary.Select(element => new XElement(element.Key, element.Value)))));
            dictionary.Clear();
        }
        xml.Save(xmlLogFileName);
        Flushed = true;
        LogBufferCounter = 0;
    }
    catch
    {
        Flushed = false;
    }
} while (Flushed == false);

当我使用值来自对象的字符串数组时,它会给出 System.Xml.Exception。手动填充(“值”+计数)与使用填充数组有什么区别?

标签: c#xml

解决方案


我删除了 try catch 块以获取异常的详细信息。感谢您向@Cee McSharpface 和@Marc Gravell 询问详细信息♦</p>

这是关于我的阵列,你们是对的。在我的数组中,一个字符串以数字开头,它给出了 XML 错误。

问题解决了再次感谢。


推荐阅读