C#——LINQ to XML(创建 XML 树)

using System.Xml.Linq;

        static void Main(string[] args)
        {
                    XElement contacts =
        new XElement("Contacts",
            new XElement("Contact",
                new XElement("Name", "Patrick Hines"),
                new XElement("Phone", "206-555-0144",
                    new XAttribute("Type", "Home")),
                new XElement("phone", "425-555-0145",
                    new XAttribute("Type", "Work")),
                new XElement("Address",
                    new XElement("Street1", "123 Main St"),
                    new XElement("City", "Mercer Island"),
                    new XElement("State", "WA"),
                    new XElement("Postal", "68042")
                )
            )
        );
            contacts.Save("Contacts.xml");
        }
<?xml version="1.0" encoding="utf-8"?>
<Contacts>
  <Contact>
    <Name>Patrick Hines</Name>
    <Phone Type="Home">206-555-0144</Phone>
    <phone Type="Work">425-555-0145</phone>
    <Address>
      <Street1>123 Main St</Street1>
      <City>Mercer Island</City>
      <State>WA</State>
      <Postal>68042</Postal>
    </Address>
  </Contact>
</Contacts>

 

posted @ 2024-09-18 16:10  echo-efun  阅读(21)  评论(0)    收藏  举报