编写一个返回一些 xml 的函数?

发布于 2024-10-15 18:35:26 字数 2151 浏览 2 评论 0原文

这可能是一个非常新手的问题,但我想做的是编写一个返回类似 XMLWriter 的函数,然后将其内容添加到另一个 xmlwriter。

例如:

  XmlWriter ToXML()
    {
        XmlWriterSettings oSettings = new XmlWriterSettings();
        oSettings.Indent = true;
        oSettings.OmitXmlDeclaration = false;
        oSettings.Encoding = Encoding.Unicode;
        Stream output = Stream.Null;

        XmlWriter writer = XmlWriter.Create(output, oSettings);
        {
            writer.WriteStartDocument(true);
            writer.WriteComment("This BaseSprite was created by the in-phone level editor");

            writer.WriteStartElement("testelement");

            writer.WriteStartAttribute("Name");
            writer.WriteValue("John Howard");
            writer.WriteEndAttribute();

            writer.WriteEndElement();
        }

        return writer;
    }

   void SomeOtherFunction()
   {
      XMLWriter xmlthing;

   // add xml things to it

   xmlthing +=  ToXML(); // now the contents of ToXML has been added in to xmlthing
  }

这可能吗?

*问题更新:

XmlWriter writer;
        XDocument doc = new XDocument();
        {
            writer = doc.CreateWriter();

            writer.WriteStartDocument(true);
            writer.WriteComment("This BaseSprite was created by the in-phone level editor");

            writer.WriteStartElement("testelement");

            writer.WriteStartAttribute("Name");
            writer.WriteValue("John Howard");
            writer.WriteEndAttribute();

            writer.WriteEndElement();

            writer.Close();
        }

        XDocument doc2 = new XDocument();
        {
            writer = doc2.CreateWriter();

            writer.WriteStartDocument(true);

            writer.WriteStartElement("testnestedelement");

            writer.WriteStartAttribute("DUUUUUDE");
            writer.WriteValue("WHERES MY CAR!?");
            writer.WriteEndAttribute();

            writer.WriteEndElement();

            writer.Close();
        }

        doc.Element("testelement").Add(doc2); // how can I make it so that doc2 is added as a nested element in 'testlement' from doc?

This is probably a very newbie question but what I'm trying to do is write a function which returns something like XMLWriter and then adds its contents to another xmlwriter.

For example:

  XmlWriter ToXML()
    {
        XmlWriterSettings oSettings = new XmlWriterSettings();
        oSettings.Indent = true;
        oSettings.OmitXmlDeclaration = false;
        oSettings.Encoding = Encoding.Unicode;
        Stream output = Stream.Null;

        XmlWriter writer = XmlWriter.Create(output, oSettings);
        {
            writer.WriteStartDocument(true);
            writer.WriteComment("This BaseSprite was created by the in-phone level editor");

            writer.WriteStartElement("testelement");

            writer.WriteStartAttribute("Name");
            writer.WriteValue("John Howard");
            writer.WriteEndAttribute();

            writer.WriteEndElement();
        }

        return writer;
    }

   void SomeOtherFunction()
   {
      XMLWriter xmlthing;

   // add xml things to it

   xmlthing +=  ToXML(); // now the contents of ToXML has been added in to xmlthing
  }

Is this possible?

*Question updated:

XmlWriter writer;
        XDocument doc = new XDocument();
        {
            writer = doc.CreateWriter();

            writer.WriteStartDocument(true);
            writer.WriteComment("This BaseSprite was created by the in-phone level editor");

            writer.WriteStartElement("testelement");

            writer.WriteStartAttribute("Name");
            writer.WriteValue("John Howard");
            writer.WriteEndAttribute();

            writer.WriteEndElement();

            writer.Close();
        }

        XDocument doc2 = new XDocument();
        {
            writer = doc2.CreateWriter();

            writer.WriteStartDocument(true);

            writer.WriteStartElement("testnestedelement");

            writer.WriteStartAttribute("DUUUUUDE");
            writer.WriteValue("WHERES MY CAR!?");
            writer.WriteEndAttribute();

            writer.WriteEndElement();

            writer.Close();
        }

        doc.Element("testelement").Add(doc2); // how can I make it so that doc2 is added as a nested element in 'testlement' from doc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狂之美人 2024-10-22 18:35:26

如果您需要在应用程序的许多功能中组合 Xml,我更愿意使用 XmlDocument。
http://msdn.microsoft.com/en-us/library /system.xml.xmldocument.aspx
或 Silverlight 中的 XDocument:
http://msdn .microsoft.com/en-us/library/system.xml.linq.xdocument%28v=VS.95%29.aspx
然后您创建一个单个 XDocument 或XmlDocument 并将其传递给操作它所需的所有函数。

I would prefer to use XmlDocument if you need to compose the Xml among many function in your app.
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
or XDocument in Silverlight:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument%28v=VS.95%29.aspx
then you create a single XDocument or XmlDocument and you pass it across all the functions needed to manipulate it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文