转换字典< string,string>通过异常“名称"到xml的“名称"不能以"1"字符,十六进制值0x31开头. " [英] Convert Dictionary<string, string> to xml through exception "Name cannot begin with the '1' character, hexadecimal value 0x31. "

查看:121
本文介绍了转换字典< string,string>通过异常“名称"到xml的“名称"不能以"1"字符,十六进制值0x31开头. "的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试将Dictionary转换为xml字符串的代码.

XElement xml_code = new XElement("root", myDictionary.Select(item => new XElement(item.Key, item.Value)));

上面的代码抛出错误

名称不能以"1"字符(十六进制值0x31)开头.

字典快照

解决方案

这可能是由于字典中的一个或多个条目违反了xml命名约定而引起的.

示例:

在这里,我转载了您面临的问题.

考虑此源代码

static void Main(string[] args)
{
    Dictionary<string, string> myList = new Dictionary<string, string>();

    myList.Add("Fruit", "Apple");
    myList.Add("Vegtable", "Potato");
    myList.Add("Vehicle", "Car");

    XElement ele = new XElement("root", myList.Select(kv => new XElement(kv.Key, kv.Value)));

    Console.WriteLine(ele);

    Console.Read();
}

您将获得输出

<root>
      <Fruit>Apple</Fruit>
      <Vegtable>Potato</Vegtable>
      <Vehicle>Car</Vehicle>
</root>

现在让我们通过将数字值放在字典条目中键值的开头来修改代码,即将"vegtable"编写为"1vegtable"

static void Main(string[] args)
{
    Dictionary<string, string> myList = new Dictionary<string, string>();

    myList.Add("Fruit", "Apple");
    //Inserting 1 before vegtable
    myList.Add("1Vegtable", "Potato");
    myList.Add("Vehicle", "Car");

    XElement ele = new XElement("root", myList.Select(kv => new XElement(kv.Key, kv.Value)));

    Console.WriteLine(ele);

    Console.Read();
}

对于此代码,我遇到以下异常

名称不能以"1"字符(十六进制值0x31)开头.

说明: 正如您在第一个代码中看到的那样,字典的键条目仅包含字母.在这种情况下,我们得到由字典条目作为xml组成的适当输出.而在第二个代码中,我通过将键输入"vegtable"设为"1vegtable"进行了一些细微的更改,但出现了一个异常.

此问题的原因在于Xml命名约定,根据Xml命名约定,Xml节点的名称不能以数字开头.由于字典的键值存储为Xml节点,因此我们得到了例外.您的源代码也是如此.

有关更多信息,请阅读以下文章:

名称不能以"1"字符(十六进制值0x31)开头.第2行,位置2

Xml标准

Here is the code that I tried to convert Dictionary to xml string.

XElement xml_code = new XElement("root", myDictionary.Select(item => new XElement(item.Key, item.Value)));

Above code throw the error

Name cannot begin with the '1' character, hexadecimal value 0x31.

snapshot of dictionary

解决方案

This might be happening because one or more entries in your dictionary is violating xml naming convention.

Example:

Here, I have reproduced the issue you are facing.

Consider this source code

static void Main(string[] args)
{
    Dictionary<string, string> myList = new Dictionary<string, string>();

    myList.Add("Fruit", "Apple");
    myList.Add("Vegtable", "Potato");
    myList.Add("Vehicle", "Car");

    XElement ele = new XElement("root", myList.Select(kv => new XElement(kv.Key, kv.Value)));

    Console.WriteLine(ele);

    Console.Read();
}

You will get output

<root>
      <Fruit>Apple</Fruit>
      <Vegtable>Potato</Vegtable>
      <Vehicle>Car</Vehicle>
</root>

Now let's modify code by putting numeric value in the start of key's value in dictionary entry i.e. writing "vegtable" as "1vegtable"

static void Main(string[] args)
{
    Dictionary<string, string> myList = new Dictionary<string, string>();

    myList.Add("Fruit", "Apple");
    //Inserting 1 before vegtable
    myList.Add("1Vegtable", "Potato");
    myList.Add("Vehicle", "Car");

    XElement ele = new XElement("root", myList.Select(kv => new XElement(kv.Key, kv.Value)));

    Console.WriteLine(ele);

    Console.Read();
}

For this code I got following exception

Name cannot begin with the '1' character, hexadecimal value 0x31.

Explanation: As you can see in the first code key entries of dictionary contain only alphabets. In this case we get a proper output consisting of dictionary entries as xml. Whereas in second code I have made a slight change by starting key entry "vegtable" as "1vegtable" and we got an exception.

The reason for this issue lies in Xml naming convention according to which name of a Xml node cannot start with numeric value. Since key values of dictionary are stored as Xml node, we are getting exception. Same is the case with your source code.

For more information read following post:

Name cannot begin with the '1' character, hexadecimal value 0x31. Line 2, position 2

Xml Standards

这篇关于转换字典&lt; string,string&gt;通过异常“名称"到xml的“名称"不能以"1"字符,十六进制值0x31开头. &quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆