Here is an example using the XMLTextWriter. It will build a hierarchical xml text representation of some data, at the end we will convert it to an XDocument object.
// fetch some data Models.ProductGroups group = _dataContext.GetGroupById(id); // declare objects StringWriter sw = new StringWriter(); XmlTextWriter writer = new XmlTextWriter(sw); writer.WriteStartElement("Product"); writer.WriteAttributeString("Id", group.id.ToString()); writer.WriteElementString("Title", group.Name); writer.WriteStartElement("Products"); // View All Products writer.WriteStartElement("Product"); writer.WriteAttributeString("Id", "0"); writer.WriteAttributeString("DisplayOrder", "0"); writer.WriteElementString("Title", "View All"); // close product writer.WriteEndElement(); // close console writer.WriteEndElement(); writer.Close(); // now we will have a string with the xml formatted data in it. string xml = sw.ToString(); // To convert this xml string to an XDocument do the following XDocument xmlDoc = XDocument.Parse(xml);
|
No responses found. Be the first to respond and make money from revenue sharing program.
|