Reputation: 255
I have a xml-file which I want to read. How can I do it? I would not load whole xml file at runtime
(XmlDocument _xd = XmlDocument.Load(path))
I want to do it with Readers, but I can not achieve with it.
At the same time I want to add nodes to this xml-file with writers. How do these work with XDocument or at c# 3.5.
Kind Regards.
Upvotes: 0
Views: 7750
Reputation: 32910
Didn't you consider also using LINQ? Like this (just pseudo-like, you would have to look it up on the web, but just to have an idea).
XDocument xmlDoc = //load or parse with XDocument.Load(..) or XDocument.Parse(...)
List<MyObject> myObj = (from myObject in xmlDoc.Descendants("myObjectTag")
select new MyObject
{
Name = (string)myObject.Attribute("name"),
...
}
).toList<MyObject>();
Voilà, here's a blog post I just found by quickly googling: * click *
Upvotes: 0
Reputation: 48088
Here is an example of loading xml file with XDocument, adding a child node and saving it to the use :
// Load XML file :
XDocument xdoc = XDocument.Load(path);
// Parse XML :
//XDocument xdoc = XDocument.Parse("<YourRootElement><ChildElement>Child 1</ChildElement></YourRootElement>");
// Add Child Node to loaded xml :
xdoc.Element("YourRootElement").Add(
new XElement("ChildElement", "Child 2"));
// Save XML to file :
xdoc.Save(path);
EDIT : Use XDocument.Parse method to load XML from memory.
Upvotes: 1
Reputation: 83254
I think this is useful.
Here's an example:
using System;
using System.Xml;
namespace ReadXml1
{
class Class1
{
static void Main(string[] args)
{
// Create an isntance of XmlTextReader and call Read method to read the file
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
textReader.Read();
// If the node has value
while (textReader.Read())
{
// Move to fist element
textReader.MoveToElement();
Console.WriteLine("XmlTextReader Properties Test");
Console.WriteLine("===================");
// Read this element's properties and display them on console
Console.WriteLine("Name:" + textReader.Name);
Console.WriteLine("Base URI:" + textReader.BaseURI);
Console.WriteLine("Local Name:" + textReader.LocalName);
Console.WriteLine("Attribute Count:" + textReader.AttributeCount.ToString());
Console.WriteLine("Depth:" + textReader.Depth.ToString());
Console.WriteLine("Line Number:" + textReader.LineNumber.ToString());
Console.WriteLine("Node Type:" + textReader.NodeType.ToString());
Console.WriteLine("Attribute Count:" + textReader.Value.ToString());
}
}
}
}
And here's an example for XMLWriters:
using System.Xml;
class Program
{
class Employee
{
int _id;
string _firstName;
string _lastName;
int _salary;
public Employee(int id, string firstName, string lastName, int salary)
{
this._id = id;
this._firstName = firstName;
this._lastName = lastName;
this._salary = salary;
}
public int Id { get { return _id; } }
public string FirstName { get { return _firstName; } }
public string LastName { get { return _lastName; } }
public int Salary { get { return _salary; } }
}
static void Main()
{
Employee[] employees = new Employee[4];
employees[0] = new Employee(1, "David", "Smith", 10000);
employees[1] = new Employee(3, "Mark", "Drinkwater", 30000);
employees[2] = new Employee(4, "Norah", "Miller", 20000);
employees[3] = new Employee(12, "Cecil", "Walker", 120000);
using (XmlWriter writer = XmlWriter.Create("employees.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("Employees");
foreach (Employee employee in employees)
{
writer.WriteStartElement("Employee");
writer.WriteElementString("ID", employee.Id.ToString());
writer.WriteElementString("FirstName", employee.FirstName);
writer.WriteElementString("LastName", employee.LastName);
writer.WriteElementString("Salary", employee.Salary.ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
}
Upvotes: 2