How to load XML “row” into index of listbox C#

2019-07-18 03:27发布

问题:

I'm trying to load a single "row" of xml to a listbox index.

<?xml version="1.0" encoding="utf-8"?>
<!--User | 4/16/2013 @ 10:05 PM-->
<ContactBook>
<Contact>
<Name>
   <Name>personName</Name>
</Name>
<City>
   <City>testCity</City>
</City>
<State>
   <State>testState</State>
</State>
<Phone>
   <Phone>testPhone</Phone>
</Phone>
</Contact>
</ContactBook>

So I would like to make name, city, state, and phone show up on one line in a listbox, then have the next contact appear in the next listbox row.

I would also like to know if anyone has some good sites that show tutorials on how to update XML files, rather than overwrite them.

/EDIT due to question in a comment/

I didn't really know where to start. My xml file is written from a dialog (a few textboxes in a class) and the this.Tag updates the listbox, and saves to the xml file. However, if a second set of contact info is saved, it overwrites the first one, rather than appending it.

/EDIT/

Thanks to LexeRoy, I've gained a bit more understanding, however, I'm still hung up.

private void updateXml_Click(object sender, EventArgs e)
{
    var xDoc = XDocument.Load(book);
    var listBoxItems = xDoc.Elements("Contact");
    listBox1.Items.Add(listBoxItems);
}

where book is a variable representing my path to the xml file and listBox1 is obvious. Unfortunately, on button click, the first row gets this value:

System.Xml.Linq.XContainer+<GetElements>d_11 

Now that the datasource has been declared, I get a thrown exception:

回答1:

You can use XDocument where you can represent your XML document and iterate each of its items. For your example,

You can create an object for a Contact where you will have a Name, City, State, and Phone properties.

var xDoc = XDocument.Load("ContactBook.xml");
var listBoxItems = xDoc.Elements("Contact"); //This will be a list of IEnumerable<XElement> child elements of Contact xml tag

Then you can manipulate those things via your code.

EDIT:

listBox1.DataSource = listBoxItems.ToList();
listBox1.DisplayMember = "Value";
listBox1.ValueMember = "Value";


回答2:

This is the way I load XML:

System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml(HttpContext.Current.Server.MapPath("~/App_Data/Lang.xml"));

Hope this helps you.



回答3:

If what you want is to display the "members" (and their values I guess) of some "Contact" object in a listbox, one approach could be to create a class for the "Contact" object and another for its "members":

class Contact
{
    public IList<ObjectMember> Members { get; set; }
}

class ObjectMember
{
    public string Name { get; set; }
    public string Value { get;set; }
}

These classes can probably help handling the fields in the dialog, too.

Then as you read the xml you instantiate a new ObjectMember which you Add to your list of members (or whatever works out to give you that!). Linq to Xml is probably the simplest and most enjoyable way to go.

When you have all members, you can bind your ListBox to the contact's Members list, but the implementation of that depends on whether you're in the WinForms or in the WPF realm.



回答4:

There is a easier method to bind by using Dictionary object

var dic = (from order in ds.Tables[0].AsEnumerable()
                   select new
                   {
                       UserView = order.Field<String>("Value"),
                       DevView = order.Field<String>("id")

                   }).AsEnumerable().ToDictionary(k => k.DevView, v => v.UserView);
                    ListBox1.DataSource = dic;
                    ListBox1.DataTextField = "Value";
                    ListBox1.DataValueField = "key";
                    ListBox1.DataBind();

Reference Link