0

I am making a WPF that searches through an XML file pulling out restaurant information. The XML is in this format:

    <FoodPhoneNumbers>
      <Restaurant Name="Pizza Place">
        <Type>Pizza</Type>
        <PhoneNumber>(123)-456-7890</PhoneNumber>
        <Hours>
          <Open>11:00am</Open>
          <Close>11:00pm</Close>
        </Hours>
      </Restaurant>
    </FoodPhoneNumbers>

I want to be able to add a new restaurant to the XML file. I have a textbox for the restaurant name, and type. Then three textboxes for the phone number. 4 comboboxes for the open hour, open minute, close hour, and close minute. I also have 2 listboxes for selecting AM or PM for the open and close times.

I assume I use XmlTextWriter, but I could not figure out how to add the text to a pre-existing XML file.

2 Answers 2

11

The simplest way isn't to use XmlTextWriter - it's just to load the whole into an in-memory representation, add the new element, then save. Obviously that's not terribly efficient for large files, but it's really simple if you can get away with it. For example, using XDocument:

XDocument doc = XDocument.Load("test.xml");
XElement restaurant = new XElement("Restaurant",
    new XAttribute("Name", "Frenchies"),
    new XElement("Type", "French"),
    new XElement("PhoneNumber", "555-12345678"),
    new XElement("Hours",
         new XElement("Open", "1:00pm"),
         new XElement("Close", "2:00pm")));
doc.Root.Add(restaurant);
doc.Save("test.xml");

Or, better:

XDocument doc = XDocument.Load("test.xml");
Restaurant restaurant = ...; // Populate a Restaurant object

// The Restaurant class could know how to serialize itself to an XElement
XElement element = restaurant.ToXElement();  

doc.Root.Add(element);
Sign up to request clarification or add additional context in comments.

2 Comments

I was writing the same but it's impossible to answer faster))) My respect to Jon, who just help anybody to solve their problem, even if question was to simple, while others minus voting.
Thank you so much. This was extremely easy and helpful! Sorry if it was a noob question!
2

The instance of XmlSerializer class can be used to achieve this.

[XmlRoot("FoodPhoneNumbers")]
public class FoodPhoneNumbers
{
    [XmlElement(ElementName = "Restaurant")]
    public List<Restaurant> Restaurants { get; set; }
}

public class Restaurant
{
    [XmlAttribute]
    public string Name { get; set; }

    [XmlElement]
    public string Type { get; set; }

    [XmlElement]
    public string PhoneNumber { get; set; }

    [XmlElement(ElementName = "Hours")]
    public List<Hours> Hours { get; set; }
}

public class Hours
{
    [XmlElement]
    public string Open { get; set; }

    [XmlElement]
    public string Close { get; set; }
}

Serialization/deserialization code:

// Deserialize.
FoodPhoneNumbers food;
using (Stream inputStream = File.OpenRead(inputFilePath))
    food = (FoodPhoneNumbers) xmlSerializer.Deserialize(inputStream);

// Add a new one.
Restaurant restaurant = new Restaurant
    {
        Name = "Coffee restraurant",
        PhoneNumber = "0xFF",
        Type = "Coffee shop"
    };
food.Restaurants.Add(restaurant);

// Serialize.
using (Stream outputStream = File.OpenWrite(outputFilePath))
    xmlSerializer.Serialize(outputStream, food);

2 Comments

You beat me to it. Here is my example: rhyous.com/2010/04/29/… I recommend XML Serialization over XDocument.
@Rhyous, good article: simple example to get started with XmlSerializer class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.