名称不能以“1”字符开始,即十六进制值0x31。在读取xml文件时

时间:2021-06-22 15:51:59

I am using an xml file to read the contents and display it in a tree view list with checkboxes. The condition for this is that the contents should be displayed on the basis of what the user selected in combo box. Suppose the user selected 2 in combo box, then the treeview list should display the contents of 2(from xml file). I have tried like:

我正在使用一个xml文件读取内容,并将其显示在带有复选框的树视图列表中。这样做的条件是,内容应该根据用户在组合框中选择的内容显示。假设用户在组合框中选择了2,那么treeview列表应该显示2的内容(来自xml文件)。我一直喜欢:

private void pjctsel_cmbbox_SelectedIndexChanged(object sender, EventArgs e)
{            
    var xmldoc = File.ReadAllText(@"D:\\test.xml");
    var str = XElement.Parse(xmldoc);
    cmbbox_val = pjctsel_cmbbox.SelectedIndex.ToString();
***  var res = str.Elements(cmbbox_val).Where(x => x.Element("general").Value.Equals(cmbbox_val)).ToList();
    MessageBox.Show(res.ToString());
}
cmbbox_val = user selected value from combobox.

The xmlfile content is:

xmlfile内容是:

<serv>
    <general name="one">    
    <server name="oneone">
        <service name="1143"/>
        <service name="1142"/>
    </server>
</general>
<general name="two">        
    <server name ="twoone">
        <service name="2143"/>
        <service name="2142"/>
    </server>
</general>
</serv>

In my c# code, where I marked * I am getting the following exception "Name cannot begin with the '1' character, hexadecimal value 0x31."

在我的c#代码中,我标记了*,我得到了以下异常“名称不能以‘1’字符开头,十六进制值0x31。”

Googled it, but I could find only those starting their xml files with tag string 1.

google了一下,但是我只能找到那些用标记字符串1启动xml文件的人。

Any ideas on this?

有什么想法吗?

Any thoughts would be really appreciated..

任何想法都将受到感激。

EDIT:

编辑:

My combo box has values like one,two.

我的组合框的值是1和2。

What I am trying is that if the user selects the value two in combo box, then my application needs to check for an entry with the name two in the xml file and if found any match, then the "server name" node and "service name"nodes corresponding to two, must be displayed in the treeview list.

我想的是,如果用户选择的值两个组合框,然后我的应用程序需要检查一个条目的名称两个xml文件,如果发现任何匹配,那么“服务器名称”节点,节点对应于两个“服务名称”,必须显示在树状视图列表。

Hope this makes sense..

希望这是有意义的。

3 个解决方案

#1


3  

cmbbox_val = pjctsel_cmbbox.SelectedIndex.ToString();   // SelectedIndex is an integer

var res = str
            .Elements(cmbbox_val)                      // so this will fail
            .Where(x => x.Element("general")
            .Value.Equals(cmbbox_val)).ToList();

This might work:

这可能工作:

cmbbox_val = pjctsel_cmbbox.SelectedItem.ToString();   // or SelectedItem.SomeProperty

But I also note that you are looking for cmbbox_val 2 times and that Element("general") already is the root of your XML. So this can't work but we don't have the information to fix it.

但我也注意到,您正在查找cmbbox_val 2次,而这个元素(“general”)已经是XML的根了。这是行不通的,但我们没有信息来修正它。


After the edit:

编辑:

  1. My combo box has values like one,two.
  2. 我的组合框的值是1和2。
  3. needs to check for an entry with the name two in the xml file
  4. 需要检查xml文件中名称为2的条目
  5. then the "server name" node and "service name"nodes must be displayed in the treeview list.
  6. 然后“服务器名”节点和“服务名”节点必须显示在treeview列表中。

Step 1) and 2)

步骤1)和2)

var str = XElement.Parse(xmldoc);
IEnumerable<XElement> generals = str
       .Elements("general")
       .Where(g => g.Attribute("name") == cmbbox_val);

and because that result is hierarchical, I would use it with foreach() instead of Linq, like so:

因为这个结果是分等级的,我将用foreach()而不是Linq来使用它:

foreach(var general in generals)  // probably only 1
{
   foreach (var server in general.Elements("server"))
   {
       string serverName = server.Attribute("name").value;

       foreach(var service  in server.Elements("service"))
       {
           // etc
       }
   }
}

#2


1  

According to MSDN XElement.Elements() takes as a parameter a string that represents the name of the element to be selected. Names can't begin with 1 and you get that error because you're passing cmbbox_val for Elements().

根据MSDN XElement.Elements(), elements()以一个字符串作为参数,该字符串表示要选择的元素的名称。名称不能以1开头,您会得到这个错误,因为您正在为元素()传递cmbbox_val。

You are using that cmbbox_val for both the Value.Equals and as the node selector: I bet it contains the string "1143"

您正在对这两个值使用cmbbox_val。等于和作为节点选择器:我打赌它包含字符串“1143”

#3


0  

The problem is that you are passing an integer as XElement name. The name should not begin with a digit. Possible mistake is that in your code you pass combobox.SelectedIndex. If you have configured the combobox properly(i.e 1,"one" 2,"two) you should pass combobox.SelectedValue. If you don't fill the value list of the combobox you can change the code as:

问题是您正在传递一个整数作为XElement名称。名称不应该以数字开头。可能的错误是在您的代码中通过了combobox.SelectedIndex。如果您正确地配置了组合框(i)。e 1,“1”2,“2”)您应该传递combobox.SelectedValue。如果您不填写组合框的值列表,可以将代码更改为:

private void pjctsel_cmbbox_SelectedIndexChanged(object sender, EventArgs e)
    {            
        var xmldoc = File.ReadAllText(@"D:\\test.xml");
        var str = XElement.Parse(xmldoc);
        string cmbbox_val = pjctsel_cmbbox.SelectedIndex==0 ? "one" : "two";
        var res = str.Elements(cmbbox_val).Where(x => x.Element("general").Value.Equals(cmbbox_val)).ToList();
        MessageBox.Show(res.ToString());
    }

#1


3  

cmbbox_val = pjctsel_cmbbox.SelectedIndex.ToString();   // SelectedIndex is an integer

var res = str
            .Elements(cmbbox_val)                      // so this will fail
            .Where(x => x.Element("general")
            .Value.Equals(cmbbox_val)).ToList();

This might work:

这可能工作:

cmbbox_val = pjctsel_cmbbox.SelectedItem.ToString();   // or SelectedItem.SomeProperty

But I also note that you are looking for cmbbox_val 2 times and that Element("general") already is the root of your XML. So this can't work but we don't have the information to fix it.

但我也注意到,您正在查找cmbbox_val 2次,而这个元素(“general”)已经是XML的根了。这是行不通的,但我们没有信息来修正它。


After the edit:

编辑:

  1. My combo box has values like one,two.
  2. 我的组合框的值是1和2。
  3. needs to check for an entry with the name two in the xml file
  4. 需要检查xml文件中名称为2的条目
  5. then the "server name" node and "service name"nodes must be displayed in the treeview list.
  6. 然后“服务器名”节点和“服务名”节点必须显示在treeview列表中。

Step 1) and 2)

步骤1)和2)

var str = XElement.Parse(xmldoc);
IEnumerable<XElement> generals = str
       .Elements("general")
       .Where(g => g.Attribute("name") == cmbbox_val);

and because that result is hierarchical, I would use it with foreach() instead of Linq, like so:

因为这个结果是分等级的,我将用foreach()而不是Linq来使用它:

foreach(var general in generals)  // probably only 1
{
   foreach (var server in general.Elements("server"))
   {
       string serverName = server.Attribute("name").value;

       foreach(var service  in server.Elements("service"))
       {
           // etc
       }
   }
}

#2


1  

According to MSDN XElement.Elements() takes as a parameter a string that represents the name of the element to be selected. Names can't begin with 1 and you get that error because you're passing cmbbox_val for Elements().

根据MSDN XElement.Elements(), elements()以一个字符串作为参数,该字符串表示要选择的元素的名称。名称不能以1开头,您会得到这个错误,因为您正在为元素()传递cmbbox_val。

You are using that cmbbox_val for both the Value.Equals and as the node selector: I bet it contains the string "1143"

您正在对这两个值使用cmbbox_val。等于和作为节点选择器:我打赌它包含字符串“1143”

#3


0  

The problem is that you are passing an integer as XElement name. The name should not begin with a digit. Possible mistake is that in your code you pass combobox.SelectedIndex. If you have configured the combobox properly(i.e 1,"one" 2,"two) you should pass combobox.SelectedValue. If you don't fill the value list of the combobox you can change the code as:

问题是您正在传递一个整数作为XElement名称。名称不应该以数字开头。可能的错误是在您的代码中通过了combobox.SelectedIndex。如果您正确地配置了组合框(i)。e 1,“1”2,“2”)您应该传递combobox.SelectedValue。如果您不填写组合框的值列表,可以将代码更改为:

private void pjctsel_cmbbox_SelectedIndexChanged(object sender, EventArgs e)
    {            
        var xmldoc = File.ReadAllText(@"D:\\test.xml");
        var str = XElement.Parse(xmldoc);
        string cmbbox_val = pjctsel_cmbbox.SelectedIndex==0 ? "one" : "two";
        var res = str.Elements(cmbbox_val).Where(x => x.Element("general").Value.Equals(cmbbox_val)).ToList();
        MessageBox.Show(res.ToString());
    }