Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# xml错误代码文件_C#_.net_Xml - Fatal编程技术网

C# xml错误代码文件

C# xml错误代码文件,c#,.net,xml,C#,.net,Xml,我们正在考虑使用xml文件来存储C应用程序的错误代码。对于以下两种结构,哪一种更好 <error> <code>0x00000001</code> <description>Error PIB: Clamp</description> <reaction>Display</reaction> </error> 或 还是你更好的建议 第二个似乎节省了一些空间,更容易通过它进行查找 我需要通过查找错误代

我们正在考虑使用xml文件来存储C应用程序的错误代码。对于以下两种结构,哪一种更好

<error>
<code>0x00000001</code>
<description>Error PIB: Clamp</description>
<reaction>Display</reaction>
</error>

还是你更好的建议

第二个似乎节省了一些空间,更容易通过它进行查找

我需要通过查找错误代码来获得描述和反应字符串。您有通过文件进行查找的有效方法吗?

如果您想了解节省空间的技术,我建议您首先不要使用XML:

就我个人而言,我更喜欢第二种方法而不是第一种,但两者都会起作用。如果这只是为了查找,我建议在加载文件时,将其转换为字典或类似的内容。您可以将键值改为数字类型,但这当然需要解析该值

LINQ到XML的转换非常容易:

XDocument xml = XDocument.Load("errorcodes.xml");
var dictionary = xml.Descendants("error")
                    .ToDictionary(element => (string) element.Attribute("code"),
                                  element => new ErrorInformation(
                                      (string) element.Element("description"),
                                      (string) element.Element("reaction")));

通常,如果希望代码描述元素,请将其设置为属性。如果您希望代码是一个元素,这意味着代码还需要属性甚至子信息,则将其作为子元素


在您的代码中,选项2是最好的IMO

检查+1,xml!=节省空间。为了节省空间,可以使用JSON对象之类的东西。与XML相比,JSON中存储的信息要少得多。只是,XML在代码中更容易处理。如果空间不是那么重要,我真的很喜欢这里给出的方法。这很简单,可以理解
XDocument xml = XDocument.Load("errorcodes.xml");
var dictionary = xml.Descendants("error")
                    .ToDictionary(element => (string) element.Attribute("code"),
                                  element => new ErrorInformation(
                                      (string) element.Element("description"),
                                      (string) element.Element("reaction")));