Reputation: 703
I need to create an xml file that is supposed to look like this.
<Event id="Date - Event Name - Event Type">
<DogNumber id="dog id number">
<dogName id-"dog id number">dog name</dogName>
<dogBreed>dog breed</dogBreed>
</DogNumber>
</Event>
and then it would repeat again for another event except for different values and attributes for the elements.
<Event id="Date - Event Name - Event Type">
<DogNumber id="dog id number">
<dogName id-"dog id number">dog name</dogName>
<dogBreed>dog breed</dogBreed>
</DogNumber>
</Event>
I am new to creating XML files with C# and am having trouble properly adding the attributes to the element and getting the parent and child nodes the same as I have shown above. I need to be able to look into this file from my C# application and be able to read all of the values listed above based on the particular event and then the particular dog in each event. The criteria of which event and which dog to select will be based upon user input in a ComboBox likely. my plan was to use the getElementById
method. However, I have seen so many different ways to do this that I am having trouble deciding what would be the best and most efficient way to do this.
Upvotes: 1
Views: 1362
Reputation: 3407
I think easiest method to read/write that xml file would be using XMLSerializer. This approach also enable You to easly bind to data (if You are using WPF for UI)
Create serializable classes:
public class Event
{
[XmlAttribute]
public string id { get; set; }
[XmlElement]
public DogNumber DogNumber { get; set; }
}
public class DogNumber
{
[XmlAttribute]
public string id { get; set; }
[XmlElement]
public dogName dogName { get; set; }
[XmlElement]
public string dogBreed { get; set; }
}
public class dogName
{
[XmlAttribute]
public string id { get; set; }
[XmlTextAttribute]
public string value { get; set; }
}
and then use XmlSerializer to deserialize(example using file):
Stream input = File.OpenRead("C:\\test.xml");
XmlSerializer serialier = new XmlSerializer(typeof(Event));
Event newevent = serialier.Deserialize(input) as Event;
input.Close();
Upvotes: 4
Reputation: 286
You can try using XDocument like:
XDocument doc = new XDocument(new XElement("Event", new XAttribute("Id", youEventString),
new XElement("DogNumber", new XAttribute("id", dogId),
new XElement("dogName", new XAttribute("id", dogNumber), dogNname),
new XElement("dogBreed", dogBreed)
)));
doc.Save(filename);
Where you can substitute youEventString, dogId, dogNumber, dogName, dogBreed with the appropriate strings. Also provide filename where the file to be saved.
Good luck
Upvotes: 0
Reputation: 3308
In C# for create or read XML, you want to use XDocument class (you have sample of use in end of this msdn website)
XDocument yourXml = null;
after your treatment ( add node, etc...) if you want, you want to save your xml in file.xml
// verify if file not existing
if (!System.IO.File.Exists(@"yourName.xml"))
{
// if file not exist, create xml file.
FileStream fs = File.Create(@"yourName.xml");
fs.Close();
}
// save your xml in xml file
yourXml.Save(@"yourName.xml");
Upvotes: 0
Reputation: 11730
I am finding the best method for my needs when it comes to XML is to serialize/deserialize using classes.
For example, take the following code:
/// <summary>
/// Details on the destination of the shipment.
/// </summary>
[XmlRoot("destination")]
public class Destination
{
List<Recipient> recipient { get; set; }
}
/// <summary>
/// Recipient details.
/// </summary>
[XmlRoot("recipient")]
public class Recipient
{
/// <summary>
/// Client Id of the recipient; only used if selected as the sort criterion.
/// </summary>
/// <remarks>Truncated after 30 characters.</remarks>
[XmlElement("client-id")]
public string ClientID { get; set; }
/// <summary>
/// Name of the individual receiving the shipment.
/// </summary>
/// <remarks>Truncated after 44 characters.</remarks>
[XmlElement("contact-name")]
public string ContactName { get; set; }
/// <summary>
/// Name of the company.
/// </summary>
/// <remarks>Truncated after 44 characters.</remarks>
[XmlElement("company")]
public string Company { get; set; }
...
This will allow me to serialize the object into XML that looks like this:
<destination>
<recipient>
<client-id></client-id>
<contact-name></contact-name>
<company></company>
...
I can control how the XML is created by using the XmlRoot or XmlElement modifiers. In your case you can use the [XmlAttribute("attributeName")]
to specify attributes.
You can read more about the various XML modifiers here: http://msdn.microsoft.com/en-us/library/e123c76w.
Upvotes: 0