XElement xmlElement = new XElement("Person", new XElement("FirstName", "John"), new XElement("LastName", "Doe"), new XElement("Age", 35)); using (var writer = new StringWriter()) { xmlElement.WriteTo(writer); Console.WriteLine(writer); }
XDocument xmlDoc = new XDocument( new XElement("Root", new XElement("Child1", "Value1"), new XElement("Child2", "Value2"))); using (var stream = new FileStream("example.xml", FileMode.Create)) { xmlDoc.WriteTo(stream); }In this example, an XDocument is created with two child elements and their values. The WriteTo method is used to write the XML content of the document to a FileStream object with the provided file name and FileMode. This will write the XML content to a file named "example.xml" in the current directory. Package Library: System.Xml.Linq.