C# XML元素值以字符串形式写入,给出IO错误C

C# XML元素值以字符串形式写入,给出IO错误C,c#,xml,C#,Xml,我正在尝试保存一个日志XML文件。下面的代码示例 private class LogFunctionInfo { private string logName; public string FunctionName; public Type FunctionClassType; public int Displayed; public int Offset; public int Multiplier; public string Log

我正在尝试保存一个日志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。使用填充数组时按handValue+count填充有什么区别?

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

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


问题解决了,再次感谢。

这里的区别是Value+count与loggingNames[count]?运行此操作时,loggingNames[count]的实际值是多少?听起来至少有一个loggingNames值无法表示为xml属性,但要知道原因,了解值区域和异常详细信息消息innerexception会有所帮助。您是在尝试添加一个xml节点还是多个节点?如果只有一个节点,则使用string.Join、、array[]