简体   繁体   English

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

[英]Name cannot begin with the '1' character, hexadecimal value 0x31. while reading from an xml file

I am using an xml file to read the contents and display it in a tree view list with checkboxes. 我正在使用xml文件来读取内容并将其显示在带有复选框的树视图列表中。 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). 假设用户在组合框中选择了2,则树视图列表应显示2的内容(来自xml文件)。 I have tried like: 我尝试过:

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. 谷歌搜索它,但我只能找到那些用标签字符串1开始他们的xml文件的人。

Any ideas on this? 有什么想法吗?

Any thoughts would be really appreciated.. 任何想法都会非常感激..

EDIT: 编辑:

My combo box has values like one,two. 我的组合框的值为一,二。

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. 我正在尝试的是,如果用户在组合框中选择值2,那么我的应用程序需要检查xml文件中名称为2的条目,如果发现任何匹配,则“服务器名称”节点和“服务”名称“对应于两个的节点,必须显示在树视图列表中。

Hope this makes sense.. 希望这有道理..

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. 但我也注意到你正在寻找cmbbox_val 2次,而Element("general")已经是你的XML的根。 So this can't work but we don't have the information to fix it. 所以这不起作用,但我们没有信息来解决它。


After the edit: 编辑后:

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

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
       }
   }
}

According to MSDN XElement.Elements() takes as a parameter a string that represents the name of the element to be selected. 根据MSDN XElement.Elements()将一个字符串作为参数,该字符串表示要选择的元素的名称。 Names can't begin with 1 and you get that error because you're passing cmbbox_val for Elements() . 名称不能以1开头,因为你传递了Elements() 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作为Value.Equals和节点选择器:我敢打赌它包含string "1143"

The problem is that you are passing an integer as XElement name . 问题是您将integer作为XElement name传递。 The name should not begin with a digit. 名称不应以数字开头。 Possible mistake is that in your code you pass combobox. 可能的错误是你的代码中你通过了组合框。 SelectedIndex . SelectedIndex If you have configured the combobox properly(ie 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: 如果你已经正确配置组合框(即1,“一个” 2“,二)你应该通过下拉框。 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());
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 名称不能以“1”字符开头,十六进制值0x31。 第2行,第2位 - Name cannot begin with the '1' character, hexadecimal value 0x31. Line 2, position 2 xml.Linq:“名称不能以 &#39;?&#39; 开头字符,十六进制值 0x3F&quot; - xml.Linq: "Name cannot begin with the '?' character, hexadecimal value 0x3F" 名称不能以&#39;&#39;字符开头,十六进制值0x20 - Name cannot begin with the ' ' character, hexadecimal value 0x20 DataContractSerializer-名称不能以“。”开头 字符,十六进制值0x2E - DataContractSerializer - Name cannot begin with the '.' character, hexadecimal value 0x2E 名称不能以 '&lt;' 字符十六进制值 0x3c 开头 - Name cannot begin with the '<' character hexadecimal value 0x3c 获取System.Xml.XmlException:名称不能以&#39;&#39;字符开头,十六进制值为0x20。 第42行,位置36 - Getting System.Xml.XmlException: Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 42, position 36 XDocument错误名称不能以&#39;&lt;&#39;字符开头,十六进制值0x3C - XDocument Error Name cannot begin with the '<' character, hexadecimal value 0x3C 使用xslt时,名称不能以&#39;=&#39;字符十六进制值0x3d开头 - Name cannot begin with the '=' character hexadecimal value 0x3d when use xslt WCF服务参考 - 获取“XmlException:名称不能以&#39;&lt;&#39;字符开头,十六进制值0x3C”在客户端 - WCF Service Reference - Getting “XmlException: Name cannot begin with the '<' character, hexadecimal value 0x3C” on Client Side 将Firebase广告添加到Xamarin.Forms会导致:名称不能以&#39;$&#39;字符开头,十六进制值为0x24 - Adding Firebase Ads to Xamarin.Forms causes: Name cannot begin with the '$' character, hexadecimal value 0x24
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM