c# - 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。手动填充(“值”+计数)与使用填充数组有什么区别?
解决方案
我删除了 try catch 块以获取异常的详细信息。感谢您向@Cee McSharpface 和@Marc Gravell 询问详细信息♦</p>
这是关于我的阵列,你们是对的。在我的数组中,一个字符串以数字开头,它给出了 XML 错误。
问题解决了再次感谢。
推荐阅读
- db2 - iSeries Access ODBC Driver with unixodbc on Debian - 从 iSeries 返回无效的 UTF-8 字符
- azure-blob-storage - AZCopy,查看错误和失败的传输信息
- javascript - 创建记录时出错 XrmServiceToolkit.Rest.Create
- scala - 为什么我在 flink 会话窗口中每个键得到一个以上的窗口
- kubernetes - 使用 Kubernetes 将零中的基本 url 配置到 JupyterHub
- latex - 使用 pgfplot 旋转 yticklabels 的问题
- java - 如何在 Thymeleaf 中动态替换片段?
- c# - 使用 autofac 时,有没有办法将现有视图模型作为数据上下文传递给最近打开的对话框窗口?
- python - 如何在 pymongo 中正确设计正则表达式?
- r - 如何在选择中的 dplyr 函数中传递列参数?