简体   繁体   中英

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. 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:

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:

<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."

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

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.

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. 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
  3. then the "server name" node and "service name"nodes must be displayed in the treeview list.

Step 1) and 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(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. Names can't begin with 1 and you get that error because you're passing cmbbox_val for Elements() .

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

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(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:

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());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM