I wish to create a file somewhat like this: http://pastebin.com/89kuK8h2
There is a tag, and in this example I got from MSDN, the customer tag also has a value and still ends in <\\Customer>:
Customer CustomerID="HELLO"> // <---- This Line
I want to know how to do that
Currently I am creating tags like:
XmlNode xHeader = xDoc.CreateElement("Customer");
and appending like this:
xDoc.DocumentElement.AppendChild(xHeader);
xHeader.AppendChild(xCustomerID);
XDocument
may be a way forward here, as its API is much simpler:
var root = new XDocument("Root",
new XElement("Customers"),
new XElement("Customer",
new XAttribute("CustomerID", "HELLO"),
new XElement("CompanyName", this.CompanyName),
new XElement("ContactName", this.ContactName),
new XElement("ContactTitle", this.ContactTitle),
new XElement("Phone", this.Phone),
new XElement("FullAddress",
new XElement("Address", "..."),
new XElement("Region", "...")
)
)
);
I suggest you to use LINQ to XML. It's easy to build your xml:
var xdoc = new XDocument(
new XElement("Root",
new XElement("Customers",
new XElement("Customer",
new XAttribute("CustomerID", "HELLO"),
new XElement("CompanyName", "Great Lakes Food Market"),
new XElement("ContactName", "Howard Snyder"),
new XElement("ContactTitle", "Marketing Managerr"),
new XElement("Phone", "(503) 555-7555"),
new XElement("FullAddress",
new XElement("Address", "2732 Baker Blvd."),
new XElement("City", "Eugene"),
new XElement("Region", "OR")
new XElement("PostalCode", "97403")
new XElement("Country", "USA")
)
)
)));
Suggested reading: Creating XML Trees in C# (LINQ to XML)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.