7

私はこのような1つのxml文字列を持っています

string stxml="<Status>Success</Status>";

また、1 つの xml ドキュメントを作成しました

  XmlDocument doc = new XmlDocument();
  XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  doc.AppendChild(docNode);
  XmlNode rootNode = doc.CreateElement("StatusList");
  doc.AppendChild(rootNode);

このような出力が必要です。

  <StatusList>
  <Status>Success</Status>
  </StatusList>

どうすればこれを達成できますか

4

6 に答える 6

18

目的を達成するための非常に簡単な方法は、見過ごされがちなXmlDocumentFragmentクラスを使用することです。

  XmlDocument doc = new XmlDocument();
  XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  doc.AppendChild(docNode);
  XmlNode rootNode = doc.CreateElement("StatusList");
  doc.AppendChild(rootNode);

  //Create a document fragment and load the xml into it
  XmlDocumentFragment fragment = doc.CreateDocumentFragment();
  fragment.InnerXml = stxml;
  rootNode.AppendChild(fragment);
于 2012-04-10T07:31:06.907 に答える
1

使用Linq to XML:

string stxml = "<Status>Success</Status>";
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
             new XElement("StatusList", XElement.Parse(stxml)));
于 2012-04-10T05:34:10.993 に答える
1

XElement代わりに次のクラスを使用できます。

string stxml = "<Status>Success</Status>";
var status = XElement.Parse(stxml);
var statusList = new XElement("StatusList", status);

var output = statusList.ToString(); // looks as you'd like

statusList新しいコンテンツをファイルに書き込む場合:

statusList.Save(@"C:\yourFile.xml", SaveOptions.None);
于 2012-04-10T05:34:27.443 に答える
0
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using System.ComponentModel;


public class MyClass
{
    public static void RunSnippet()
    {
        XmlNode node = default(XmlNode);
        if(node == null)
            Console.WriteLine(bool.TrueString);
        if(node != null)
            Console.WriteLine(bool.FalseString);

        XmlDocument doc = new  XmlDocument();

        node = doc.CreateNode (XmlNodeType.Element,"Query", string.Empty);

        node.InnerXml=@"<Where><Eq><FieldRef Name=""{0}"" /><Value Type=""{1}"">{2}</Value></Eq></Where>";

        string xmlData = ToXml<XmlNode>(node);

        Console.WriteLine(xmlData);

        XmlNode node1 = ConvertFromString(typeof(XmlNode), @"<Query><Where><Eq><FieldRef Name=""{0}"" /><Value Type=""{1}"">{2}</Value></Eq></Where></Query>") as XmlNode;
        if(node1 == null)
            Console.WriteLine(bool.FalseString);
        if(node1 != null)
            Console.WriteLine(bool.TrueString);

        string xmlData1 = ToXml<XmlNode>(node1);
        Console.WriteLine(xmlData1);
    }
    public static string ToXml<T>(T t)
    {
        string Ret = string.Empty;
        XmlSerializer s = new XmlSerializer(typeof(T));
        using (StringWriter Output = new StringWriter(new System.Text.StringBuilder()))
        {
            s.Serialize(Output, t);
            Ret = Output.ToString();
        }
        return Ret;
    }
        public static object ConvertFromString(Type t, string sourceValue)
        {
            object convertedVal = null;

            Type parameterType = t;
            if (parameterType == null) parameterType = typeof(string);
            try
            {

                // Type t = Type.GetType(sourceType, true);
                TypeConverter converter = TypeDescriptor.GetConverter(parameterType);
                if (converter != null && converter.CanConvertFrom(typeof(string)))
                {
                    convertedVal = converter.ConvertFromString(sourceValue);
                }
                else
                {
                    convertedVal = FromXml(sourceValue, parameterType);
                }
            }
            catch { }
            return convertedVal;
        }
              public static object FromXml(string Xml, Type t)
        {
            object obj;
            XmlSerializer ser = new XmlSerializer(t);
            using (StringReader stringReader = new StringReader(Xml))
            {
                using (System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(stringReader))
                {
                    obj = ser.Deserialize(xmlReader);
                }
            }
            return obj;
        }

    #region Helper methods

    public static void Main()
    {
        try
        {
            RunSnippet();
        }
        catch (Exception e)
        {
            string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
            Console.WriteLine(error);
        }
        finally
        {
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
    }

    private static void WL(object text, params object[] args)
    {
        Console.WriteLine(text.ToString(), args);   
    }

    private static void RL()
    {
        Console.ReadLine(); 
    }

    private static void Break() 
    {
        System.Diagnostics.Debugger.Break();
    }

    #endregion
}
于 2013-08-08T11:48:44.153 に答える
0

あなたはxmlwriterを使ってそれを試すことができます

using (XmlWriter writer = XmlWriter.Create("new.xml"))
{
        writer.WriteStartDocument();
        writer.WriteStartElement("StatusList");
        writer.WriteElementString("Status", "Success");   // <-- These are new
        writer.WriteEndDocument();
}
于 2012-04-10T05:37:57.393 に答える