开发者

Rewriting into xml

开发者 https://www.devze.com 2023-04-11 03:48 出处:网络
I have a listbox and a textbox in my WPF. Im saving these values into an xml like this: private void textBox1_LostFocus(object sender, RoutedEventArgs e)

I have a listbox and a textbox in my WPF. Im saving these values into an xml like this:

private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
    if (listBox1.SelectedValue != null)
    {
        writer.WriteStartElement("Attribute",textBox1.Text);
        ListBoxItem a = listBox1.SelectedValue as ListBoxItem;
        if (a != null)
        {
            writer.WriteAttributeString("Name", a.Content.ToString());
        }
        //writer.WriteAttributeString("Value", textBox1.Text);
        writer.WriteEndElement();
        writer.Flush();
    }
}

But when I select an item that is already selected and saved, i want my xml to be re-written. Now i get something like this:

<?xml version="1.0" encoding="utf-8"?>
<Query_advanced>
  <Query>
    <Attribute Name="Patient Name" xmlns="John" />
    <Attribute Name="Patient Age" xmlns="23" />
    <Attribute Name="Patient ID" xmlns="12" />
    <Attribute Name="Patient Name" xmlns="Mary" />
  </开发者_JS百科Query>
</Query_advanced>

How can I go about this? Thanks!


Why not just use the XmlSerializer Class?

You could just create a Patient object. Then serialize and deserialize from it like:

[Serializable]
public class Patient
{
    public string Name {get; set;}
    public int Age {get; set;}
    public int Id {get; set;}

    public override string ToString()
    {
         return Name;
    }
}
...
public void Serialize(List<Patient> pList)
{
    using (Stream writer = new FileStream(filename, FileMode.Create))
    {
        var serializer = new XmlSerializer(typeof (List<Patient>));
        serializer.Serialize(writer, pList);
    }
}

public List<Patient> Deserialize()
{
    using (Stream reader = new FileStream(filename, FileMode.Open))
    {
        var serializer = new XmlSerializer(typeof (List<Patient>));

        var pList = (List<Patient>) serializer.Deserialize(reader);

        return pList;
    }
}

Now you can create a patient object like normal, save it with serialize, and load it back into an object with deserialize.

You might want to use it like this:

public List<Patient> Patients; // Patient collection
//Populate your listBox with these patient objects.

private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
     if (listBox1.SelectedItem == null) return;

     var patient = listBox1.SelectedItem as Patient; // Get the selected PObj;

     patient.Name = textBox1.Text; 

     Serialize(Patients); //Save the list to xml
}


private Dictionary<string, string> attributes = new Dictionary<string, string>();    

private void textBox1_LostFocus(object sender, RoutedEventArgs e)
{
    if (listBox1.SelectedValue != null) 
    {
        ListBoxItem a = listBox1.SelectedValue as ListBoxItem;
        string name = a.Content.ToString();
        string value = textBox1.Text;
        if (!this.attributes.ContainsKey(name) || this.attributes[name] != value)
        {
             this.attributes[name] = value;
             this.UpdateXml();
        }            
    }
}

private void UpdateXml()
{
    foreach(KeyValuePair<string,string> attribute in this.attributes)
    {
        writer.WriteStartElement("Attribute",textBox1.Text);
        if (attribute.Key != null)
        {
            writer.WriteAttributeString("Name", attribute.Key);
        }

        writer.WriteAttributeString("Value", attribute.Value);
        writer.WriteEndElement();
    }

    writer.Flush();
}


Taking @bitbonk's answer answer into account and modifying, this worked for me:

private void textBox1_LostFocus(object sender, RoutedEventArgs e){
    if (listBox2.SelectedValue != null){
         string name = listBox2.SelectedItem.ToString();
         string value = textBox1.Text;
         if(!attributes_dict.ContainsKey(name)){
             //attributes.Add(name, value);
             attributes_dict[name] = value;
             //UpdateXml();
         }else{
             attributes_dict.Remove(name);
             attributes_dict.Add(name, value);
             //UpdateXml();
         }
     }
}


private  void button1_Click(object sender, RoutedEventArgs e){
     foreach (KeyValuePair<string, string> attribute in this.attributes_dict){
          writer.WriteStartElement("Attribute", textBox1.Text);
          if(attribute.Key != null){
               writer.WriteAttributeString("Name1", attribute.Key);
          }
          writer.WriteAttributeString("Value1", attribute.Value);                                     
          writer.WriteEndElement();
     }
     writer.Flush();
     writer.WriteEndElement();
     writer.Flush();
     writer.WriteEndDocument();
     writer.Close();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消