Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#需要创建带有前缀且不带命名空间的XmlElement_C#_Xml_Serialization - Fatal编程技术网

C#需要创建带有前缀且不带命名空间的XmlElement

C#需要创建带有前缀且不带命名空间的XmlElement,c#,xml,serialization,C#,Xml,Serialization,我需要使用c#like创建一个序列化的xml元素 用于序列化的方法如下所示 Root arg= new Root(); arg.Name= "Jimmy"; // Hardcoding need to be removed arg.ReportPeriod = "200803"; // Hardcoding need to be removed XmlElement x = SerializeToXmlElement(arg); public stat

我需要使用c#like创建一个序列化的xml元素


用于序列化的方法如下所示

    Root arg= new Root();
    arg.Name= "Jimmy"; // Hardcoding need to be removed
    arg.ReportPeriod = "200803"; // Hardcoding need to be removed
    XmlElement x = SerializeToXmlElement(arg);

    public static XmlElement SerializeToXmlElement(object o)
   {
       XmlDocument doc = new XmlDocument();
       //XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
       //ns.Add("m2", "advisorIWDBRetrievalWSArguments");
       using (XmlWriter writer = doc.CreateNavigator().AppendChild())
       {
           new XmlSerializer(o.GetType()).Serialize(writer, o);
       }

       return doc.DocumentElement;
   }

Thnx prevance

xml是这样的一些值,一些值,你的问题是什么?这就是我想要的一些值,一些值,你可以随时编辑自己的问题。没有必要把它放到评论部分,因为Serv说我们需要更多地了解您的问题。您根本不知道如何在c#中使用XML吗?您不知道一旦拥有了XML,如何序列化它吗?它是以错误的格式发布的吗?运行代码时是否出现异常?事实上,你已经有了什么代码?
        namespace xyz
    {
    [Serializable()]
    [XmlRoot("Root", Namespace = "abc", IsNullable = false)]
    public class Root{
    [XmlElement(ElementName = "Name")]
    public string Name{ get; set; }     
    [XmlElement(ElementName = "ReportPeriod")]
    public string ReportPeriod { get; set; }        
    }
    }
    Root arg= new Root();
    arg.Name= "Jimmy"; // Hardcoding need to be removed
    arg.ReportPeriod = "200803"; // Hardcoding need to be removed
    XmlElement x = SerializeToXmlElement(arg);

    public static XmlElement SerializeToXmlElement(object o)
   {
       XmlDocument doc = new XmlDocument();
       //XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
       //ns.Add("m2", "advisorIWDBRetrievalWSArguments");
       using (XmlWriter writer = doc.CreateNavigator().AppendChild())
       {
           new XmlSerializer(o.GetType()).Serialize(writer, o);
       }

       return doc.DocumentElement;
   }