我需要使用c#like
创建一个序列化的xml元素<a:Root>
<Name>Jimmy</Name>
<ReportPeriod>200803</ReportPeriod>
</a:Root>
我无法在没有命名空间的情况下添加前缀。我不需要命名空间,因为它作为参数传递给java服务,而该服务无法以任何其他形式投射它
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;
}
Thnx提前