开发者

creating xml on asp.net? [closed]

开发者 https://www.devze.com 2023-03-29 00:06 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help cente开发者_C百科r. Closed 11 years ago.

ı wanna create xml file on aspx.cs but ı have no idea what ı need to do.can anyone help me? ı don't want to use database.just this way.

- <categories>
- <category>
  <id>0</id> 
  <name>Music</name> 
  </category>
- <category>
  <id>1</id> 
  <name>Sport</name> 
  </category>
- <category>
  <id>2</id> 
  <name>News</name> 
  </category>
  </categories>


You could use the XDocument class from the System.Xml.Linq namespace:

XDocument myXml = new XDocument(new XElement("categories",
                                   new XElement("category",
                                      new XElement("id", "0"),
                                      new XElement("name", "music")),
                                   new XElement("category",
                                      new XElement("id", "1"),
                                      new XElement("name", "Sport")),
                                   new XElement("category",
                                      new XElement("id", "2"),
                                      new XElement("name", "News"))));
string xmlString = myXml.ToString();
// Or you could save it to a file, a stream, etc:
myXml.Save("Categories.xml");

This outputs:

<categories>
    <category>
        <id>0</id>
        <name>music</name>
    </category>
    <category>
        <id>1</id>
        <name>Sport</name>
    </category>
    <category>
        <id>2</id>
        <name>News</name>
    </category> 
</categories>

XDocument Class


 protected void Page_Load(object sender, System.EventArgs e)
        {
            var str = @"<categories>
    <category>
      <id>0</id>
      <name>Music</name>
    </category>
    <category>
      <id>1</id>
      <name>Sport</name>
    </category>
    <category>
      <id>2</id>
      <name>News</name>
    </category>
  </categories>";
            Response.Write(str);
            Response.End();
        }


You could try serializing a class to XML, there is a good tutorial for that here:

http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization

0

精彩评论

暂无评论...
验证码 换一张
取 消