Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

xmldocument - C# Adding data to xml file

I'm building an Parts app in order to learn C# and WPF. I trying having trouble adding new parts using XmlWriter. I can create the xml file, but can not figure how to add additional parts. Should I be using something else like XmlDocument? Here is my code behind:

private void btnSave_Click(object sender, RoutedEventArgs e)
    {

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Encoding = Encoding.UTF8;
        settings.Indent = true;

        using (XmlWriter writer = XmlWriter.Create("f:\MyParts.xml", settings))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("MyParts");
            writer.WriteStartElement("parts");
            writer.WriteStartElement("item");
            writer.WriteString(txtbxitem.Text);
            writer.WriteEndElement();

            writer.WriteStartElement("color");
            writer.WriteString(txtbxcolor.Text);
            writer.WriteEndElement();

            writer.WriteStartElement("size");
            writer.WriteString(txtbxsize.Text);
            writer.WriteEndElement();

            writer.WriteEndElement();
            writer.WriteEndDocument();

            writer.Flush();
            writer.Close();

        }
    }

This code creates the xml file and a node correctly, but how do I add additional parts? Here is what I am trying to create:

 <?xml version="1.0" encoding="ISO-8859-1" ?> 

<MyParts>
  <parts>
    <item>Part1</item>
    <color>Red</color>
    <size>SM</size>
  </parts>
  <parts>
    <item>Part2</item>
    <color>Blue</color>
    <size>XXL</size>
  </parts>
</MyParts>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Personally I'd suggest using LINQ to XML. It's a much easier API to use than XmlDocument.

But yes, if you want to modify an existing document then typically using an in-memory representation is simpler than using a streaming API. It's possible to do the latter of course, but it's not easy.

Here's an example to create the same XML as you've already got (other than the declaration: any reason you'd want to use Latin-1 instead of something like UTF-8 which can represent the whole of Unicode, btw?)

var doc = new XDocument(
  new XElement("MyParts",
    new XElement("parts",
      new XElement("item", "Part1"),
      new XElement("color", "Red"),
      new XElement("size", "SM")),
    new XElement("parts",
      new XElement("item", "Part2"),
      new XElement("color", "Blue"),
      new XElement("size", "XXL"))));

Then if you wanted to add another part:

doc.Root.Add(
  new XElement("parts",
    new XElement("item", "Part3"),
    new XElement("color", "Green"),
    new XElement("size", "L")));

Admittedly I'd expect you'd want to encapsulate the "create a parts element" bit into a method to avoid repeating it all the time... but hopefully you get the picture.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...