Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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# 使用IXmlSerializable自定义xml输出_C#_Wcf_Xml Serialization - Fatal编程技术网

C# 使用IXmlSerializable自定义xml输出

C# 使用IXmlSerializable自定义xml输出,c#,wcf,xml-serialization,C#,Wcf,Xml Serialization,我试图实现的目标似乎相当简单 在课堂上 public class Wrapper<T> { T Data { get; set; } bool Success { get;set; } List<Error> Errors { get; set; } } - 公共类Util { 公共静态void XmlSerialize(T obj,XmlWriter) { var nsSerializer=新的XmlSerializerNamespaces();

我试图实现的目标似乎相当简单

在课堂上

public class Wrapper<T> { 
   T Data { get; set; }
   bool Success { get;set; }
   List<Error> Errors { get; set; }
}
-

公共类Util
{
公共静态void XmlSerialize(T obj,XmlWriter)
{
var nsSerializer=新的XmlSerializerNamespaces();
nsSerializer.Add(“,”);
var ser=新的XmlSerializer(typeof(T));
serial.Serialize(writer、obj、nsSerializer);
}
}

您可以使用XmlOverrides:

var xmlOverrides = new XmlAttributeOverrides();
var attributes = new XmlAttributes();
attributes.XmlElements
     .Add(new XmlElementAttribute("Person", typeof (Person)));
xmlOverrides.Add(typeof(Wrapper<Person>), "Data", attributes);

var serializer = new XmlSerializer(typeof(Wrapper<ExampleObject>), xmlOverrides);
var-xmlOverrides=new-XmlAttributeOverrides();
var attributes=新的XmlAttributes();
attributes.xml元素
.Add(新的XmlElementAttribute(“Person”,typeof(Person)));
Add(typeof(Wrapper),“Data”,属性);
var serializer=新的XmlSerializer(typeof(Wrapper),xmlOverrides);

模式

public class Wrapper<T>
{
    public T Data { get; set; }
    public bool Success { get; set; }
}


public class Person
{
    public string first;
    public string last;
}
公共类包装器
{
公共T数据{get;set;}
公共bool成功{get;set;}
}
公共阶层人士
{
公共字符串优先;
公共字符串最后;
}
XML序列化

        Wrapper<Person> f = new Wrapper<Person>();
        f.Data = new Person();
        f.Data.first = "Bob";
        f.Data.last = "Robertson";
        XmlSerializer SerializerObj = new XmlSerializer(typeof(Wrapper<Person>));
        TextWriter WriteFileStream = new StreamWriter(@"C:\test.xml");
        SerializerObj.Serialize(WriteFileStream, f);
        WriteFileStream.Close();
Wrapper f=new Wrapper();
f、 数据=新人();
f、 Data.first=“Bob”;
f、 Data.last=“罗伯逊”;
XmlSerializer SerializerObj=新的XmlSerializer(typeof(Wrapper));
TextWriter WriteFileStream=新的StreamWriter(@“C:\test.xml”);
SerializerObj.Serialize(WriteFileStream,f);
WriteFileStream.Close();
XML输出

<?xml version="1.0" encoding="utf-8"?>
<WrapperOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Data>
    <first>Bob</first>
    <last>Robertson</last>
  </Data>
  <Success>false</Success>
</WrapperOfPerson>

上下快速移动
罗伯逊
错误的

@onof:我尝试过一些不寻常的事情,但感觉自己正在走上重新实现XmlSerializer的道路(例如,尊重注释等)。我想可能有一个我忽略的简单方法。我还尝试重新构造代码以更好地匹配所需的格式,但这似乎真的很糟糕。下面是曲线球:这是在WCF中,我在服务合同中指定[XmlSerializerFormat],因此我不负责创建XmlSerializer。这就是我目前拥有的。我要找的是
BobRobertson
你只能得到数据标签或个人标签。
var xmlOverrides = new XmlAttributeOverrides();
var attributes = new XmlAttributes();
attributes.XmlElements
     .Add(new XmlElementAttribute("Person", typeof (Person)));
xmlOverrides.Add(typeof(Wrapper<Person>), "Data", attributes);

var serializer = new XmlSerializer(typeof(Wrapper<ExampleObject>), xmlOverrides);
public class Wrapper<T>
{
    public T Data { get; set; }
    public bool Success { get; set; }
}


public class Person
{
    public string first;
    public string last;
}
        Wrapper<Person> f = new Wrapper<Person>();
        f.Data = new Person();
        f.Data.first = "Bob";
        f.Data.last = "Robertson";
        XmlSerializer SerializerObj = new XmlSerializer(typeof(Wrapper<Person>));
        TextWriter WriteFileStream = new StreamWriter(@"C:\test.xml");
        SerializerObj.Serialize(WriteFileStream, f);
        WriteFileStream.Close();
<?xml version="1.0" encoding="utf-8"?>
<WrapperOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Data>
    <first>Bob</first>
    <last>Robertson</last>
  </Data>
  <Success>false</Success>
</WrapperOfPerson>