重写为xml

发布于 2024-12-08 18:21:18 字数 999 浏览 2 评论 0原文

我的 WPF 中有一个列表框和一个文本框。我将这些值保存到 xml 中,如下所示:

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

但是当我选择已选择并保存的项目时,我希望重写我的 xml。现在我得到这样的信息:

<?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" />
  </Query>
</Query_advanced>

我该如何解决这个问题?谢谢!

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" />
  </Query>
</Query_advanced>

How can I go about this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒岛晴空 2024-12-15 18:21:18

为什么不直接使用 XmlSerializer 类?

您可以只创建一个 Patient 对象。然后对其进行序列化和反序列化,如下所示:

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

现在您可以像平常一样创建一个患者对象,使用序列化保存它,然后使用反序列化将其加载回对象中。

您可能想像这样使用它:

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
}

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
}
满天都是小星星 2024-12-15 18:21:18
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();
}
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();
}
各自安好 2024-12-15 18:21:18

考虑@bitbonk的答案答案并进行修改,这对我有用:

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文