开发者

Writing a function which returns a bit of xml?

开发者 https://www.devze.com 2023-02-08 12:29 出处:网络
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.

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.Write开发者_开发知识库Value("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?


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.

0

精彩评论

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