これはおそらく非常に初心者の質問ですが、私がやろうとしているのは、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?