如何编写或生成XML文件

时间:2022-11-23 08:40:41

How to write or generate an XML file using C# to get the result below?

如何使用C#编写或生成XML文件以获得下面的结果?

<?xml version="1.0" encoding="UTF-8"?>
<pages>
<page name="Page Name 1" url="/page-1/" />
<page name="Page Name 2" url="/page-2/" />
<page name="Page Name 3" url="/page-3/" />
<page name="Page Name 4" url="/page-4/" />
</pages>

7 个解决方案

#1


10  

using System.Linq;
using System.Xml;
using System.Xml.Linq;

// ...

using (var writer = XmlWriter.Create("output.xml"))
     new XDocument(
        new XDeclaration("1.0", "UTF-8", null),
        new XElement("pages",
            Enumerable.Range(1, 4)
                .Select(i => new XElement("page",
                                  new XAttribute("name", "Page Name " + i),
                                  new XAttribute("url", "/page-" + i + "/"))))
     ).WriteTo(writer);

#2


5  

A rather straight-forward way could be like this:

一个相当直接的方式可能是这样的:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(@"c:\path\file.xml", settings))
{

    writer.WriteStartElement("pages");

    for (int i = 1; i < 5; i++)
    {
        writer.WriteStartElement("page");
        writer.WriteAttributeString("name", "Page Name " + i.ToString());
        writer.WriteAttributeString("url", string.Format("/page-{0}/", i));
        writer.WriteEndElement(); // page
    }
    writer.WriteEndElement(); // pages
}

#3


3  

I guess something like this:

我猜是这样的:

System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(Server.MapPath("pages.xml"), System.Text.Encoding.UTF8);

        writer.WriteStartDocument();
        writer.WriteStartElement("pages");

        writer.WriteStartElement("page");
        writer.WriteAttributeString("name", "Page name 1");
        writer.WriteAttributeString("url", "Page url 1");
        writer.WriteEndElement();

        writer.WriteStartElement("page");
        writer.WriteAttributeString("name", "Page name 2 ");
        writer.WriteAttributeString("url", "Page url 2");
        writer.WriteEndElement();

        writer.WriteStartElement("page");
        writer.WriteAttributeString("name", "Page name 3");
        writer.WriteAttributeString("url", "Page url 3");
        writer.WriteEndElement();

        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Close();  

#4


2  

If you are using the .NET framework 2.0, you should use the XmlWriter class to write XML, else you can use the XmlTextWriter.

如果您使用的是.NET framework 2.0,则应使用XmlWriter类编写XML,否则可以使用XmlTextWriter。

If you are on .NET 3+, you can use the XDocument class of the System.Xml.Linq namespace.

如果您使用的是.NET 3+,则可以使用System.Xml.Linq命名空间的XDocument类。

#5


1  

using System.Xml;
//...
XmlDocument xmldoc = new XmlDocument();
XmlElement pages = xmldoc.CreateElement("pages");
XmlElement page = null;

for(int i = 1; i <= 4; i++)
{
    page = xmldoc.CreateElement("page");
    page.SetAttribute("name", "Page Name " + i);
    page.SetAttribute("url", "/page-" + i + "/");
    pages.AppendChild(page);
}

xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null));
xmldoc.AppendChild(pages);
xmldoc.Save("output.xml");

#6


0  

A couple of points on the answers given. For an XML document of that size, XDocument is pretty inefficient way to handle it. At least, with my understanding of what XDocument should be used for. If your handling large documents in memory and you want to query them with LINQ, then XmlDocument/XDocument is the way forward. If I'm wrong, please correct me and accept my apologies.

给出答案的几点。对于那个大小的XML文档,XDocument是处理它的非常低效的方法。至少,我了解XDocument应该用于什么。如果您在内存中处理大型文档并且想要使用LINQ查询它们,那么XmlDocument / XDocument就是前进的方法。如果我错了,请纠正我并接受我的道歉。

Secondly, again with my understanding, your XML structure is wrong. Please do correct me if I haven't got this right but attributes are meta-data describing what the element value is. It's not to be used as the value itself. For your case, your structure should be something like..

其次,根据我的理解,您的XML结构是错误的。如果我没有这个权利,请纠正我,但属性是描述元素值是什么的元数据。它不能用作价值本身。对于你的情况,你的结构应该像......

<pages>
    <page>
        <name href="url">Page Name 1</name>
    </page>
</pages>

Or if you want to be really anal

或者,如果你想成为真正的肛门

<pages>
    <page>
        <name>Page Name 1
        <url>http://page/</url>
    </page>
</pages>

I fear this answer will open up the dreaded element vs attribute debate (which shouldn't exist but always does.)

我担心这个答案会打开可怕的元素与属性的争论(这不应该存在但总是存在。)

EDIT: This isn't a direct answer to the question; I feel it is closer to a metacomment (a comment about the answers of the question). I wasn't sure where to put it if it was even relevant but I think it's worth mentioning.

编辑:这不是问题的直接答案;我觉得它更接近一个元素(关于问题答案的评论)。如果它甚至相关,我不知道该把它放在哪里,但我认为值得一提。

#7


0  

You could also look at XML Data Binding. This approach would give you code that looks more like this.

您还可以查看XML数据绑定。这种方法可以为您提供看起来更像这样的代码。

Pages pages = new Pages();
for(int i=1; i<=4;i++)
{
    Page p = new Page();
    p.name = "Page Name " + i;
    p.url = "/page-" + i + "/";
    pages.Pages.Add(p);
}
Console.Write(pages.ToXml());

Have a look at "A closer look at XML Data Binding"

看看“仔细研究XML数据绑定”

Here is also a good list of xml data binding tools

这里还有一个很好的xml数据绑定工具列表

#1


10  

using System.Linq;
using System.Xml;
using System.Xml.Linq;

// ...

using (var writer = XmlWriter.Create("output.xml"))
     new XDocument(
        new XDeclaration("1.0", "UTF-8", null),
        new XElement("pages",
            Enumerable.Range(1, 4)
                .Select(i => new XElement("page",
                                  new XAttribute("name", "Page Name " + i),
                                  new XAttribute("url", "/page-" + i + "/"))))
     ).WriteTo(writer);

#2


5  

A rather straight-forward way could be like this:

一个相当直接的方式可能是这样的:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(@"c:\path\file.xml", settings))
{

    writer.WriteStartElement("pages");

    for (int i = 1; i < 5; i++)
    {
        writer.WriteStartElement("page");
        writer.WriteAttributeString("name", "Page Name " + i.ToString());
        writer.WriteAttributeString("url", string.Format("/page-{0}/", i));
        writer.WriteEndElement(); // page
    }
    writer.WriteEndElement(); // pages
}

#3


3  

I guess something like this:

我猜是这样的:

System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(Server.MapPath("pages.xml"), System.Text.Encoding.UTF8);

        writer.WriteStartDocument();
        writer.WriteStartElement("pages");

        writer.WriteStartElement("page");
        writer.WriteAttributeString("name", "Page name 1");
        writer.WriteAttributeString("url", "Page url 1");
        writer.WriteEndElement();

        writer.WriteStartElement("page");
        writer.WriteAttributeString("name", "Page name 2 ");
        writer.WriteAttributeString("url", "Page url 2");
        writer.WriteEndElement();

        writer.WriteStartElement("page");
        writer.WriteAttributeString("name", "Page name 3");
        writer.WriteAttributeString("url", "Page url 3");
        writer.WriteEndElement();

        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Close();  

#4


2  

If you are using the .NET framework 2.0, you should use the XmlWriter class to write XML, else you can use the XmlTextWriter.

如果您使用的是.NET framework 2.0,则应使用XmlWriter类编写XML,否则可以使用XmlTextWriter。

If you are on .NET 3+, you can use the XDocument class of the System.Xml.Linq namespace.

如果您使用的是.NET 3+,则可以使用System.Xml.Linq命名空间的XDocument类。

#5


1  

using System.Xml;
//...
XmlDocument xmldoc = new XmlDocument();
XmlElement pages = xmldoc.CreateElement("pages");
XmlElement page = null;

for(int i = 1; i <= 4; i++)
{
    page = xmldoc.CreateElement("page");
    page.SetAttribute("name", "Page Name " + i);
    page.SetAttribute("url", "/page-" + i + "/");
    pages.AppendChild(page);
}

xmldoc.AppendChild(xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null));
xmldoc.AppendChild(pages);
xmldoc.Save("output.xml");

#6


0  

A couple of points on the answers given. For an XML document of that size, XDocument is pretty inefficient way to handle it. At least, with my understanding of what XDocument should be used for. If your handling large documents in memory and you want to query them with LINQ, then XmlDocument/XDocument is the way forward. If I'm wrong, please correct me and accept my apologies.

给出答案的几点。对于那个大小的XML文档,XDocument是处理它的非常低效的方法。至少,我了解XDocument应该用于什么。如果您在内存中处理大型文档并且想要使用LINQ查询它们,那么XmlDocument / XDocument就是前进的方法。如果我错了,请纠正我并接受我的道歉。

Secondly, again with my understanding, your XML structure is wrong. Please do correct me if I haven't got this right but attributes are meta-data describing what the element value is. It's not to be used as the value itself. For your case, your structure should be something like..

其次,根据我的理解,您的XML结构是错误的。如果我没有这个权利,请纠正我,但属性是描述元素值是什么的元数据。它不能用作价值本身。对于你的情况,你的结构应该像......

<pages>
    <page>
        <name href="url">Page Name 1</name>
    </page>
</pages>

Or if you want to be really anal

或者,如果你想成为真正的肛门

<pages>
    <page>
        <name>Page Name 1
        <url>http://page/</url>
    </page>
</pages>

I fear this answer will open up the dreaded element vs attribute debate (which shouldn't exist but always does.)

我担心这个答案会打开可怕的元素与属性的争论(这不应该存在但总是存在。)

EDIT: This isn't a direct answer to the question; I feel it is closer to a metacomment (a comment about the answers of the question). I wasn't sure where to put it if it was even relevant but I think it's worth mentioning.

编辑:这不是问题的直接答案;我觉得它更接近一个元素(关于问题答案的评论)。如果它甚至相关,我不知道该把它放在哪里,但我认为值得一提。

#7


0  

You could also look at XML Data Binding. This approach would give you code that looks more like this.

您还可以查看XML数据绑定。这种方法可以为您提供看起来更像这样的代码。

Pages pages = new Pages();
for(int i=1; i<=4;i++)
{
    Page p = new Page();
    p.name = "Page Name " + i;
    p.url = "/page-" + i + "/";
    pages.Pages.Add(p);
}
Console.Write(pages.ToXml());

Have a look at "A closer look at XML Data Binding"

看看“仔细研究XML数据绑定”

Here is also a good list of xml data binding tools

这里还有一个很好的xml数据绑定工具列表