C# 从.NET将类型序列化为XML
我有这种C#4.0类型 我想使用XmlSerializer将类型序列化为C# 从.NET将类型序列化为XML,c#,.net,xmlserializer,ixmlserializable,C#,.net,Xmlserializer,Ixmlserializable,我有这种C#4.0类型 我想使用XmlSerializer将类型序列化为 <Val Estimate="true">123</Val> 123 理想情况下,如果估计属性的值为false,我希望省略它。将估算值更改为可为空的布尔值是可以接受的 从这种类型到这种XML表示需要哪些属性/实现 谢谢。不确定是否可以仅使用属性有条件地输出估算值。但您确实可以实现IXmlSerializable并检查WriteXml方法中的估计值 这里有一个有条件地省略估计值将需要一个lof编码
<Val Estimate="true">123</Val>
123
理想情况下,如果估计属性的值为false,我希望省略它。将估算值更改为可为空的布尔值是可以接受的
从这种类型到这种XML表示需要哪些属性/实现
谢谢。不确定是否可以仅使用属性有条件地输出估算值。但您确实可以实现IXmlSerializable并检查WriteXml方法中的估计值
这里有一个有条件地省略
估计值
将需要一个lof编码。我不会走那条路
XmlWriter writer = XmlWriter.Create(stream, new XmlWriterSettings() { OmitXmlDeclaration = true });
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer xml = new XmlSerializer(typeof(DecimalField));
xml.Serialize(writer, obj, ns);
-
还可以使用Linq2Xml手动序列化类
List<XObject> list = new List<XObject>();
list.Add(new XText(obj.Value.ToString()));
if (obj.Estimate) list.Add(new XAttribute("Estimate", obj.Estimate));
XElement xElem = new XElement("Val", list.ToArray());
xElem.Save(stream);
List List=新列表();
添加(新的XText(obj.Value.ToString());
if(目标估算)列表添加(新的X属性(“估算”,目标估算));
XElement xElem=新XElement(“Val”,list.ToArray());
xElem.Save(流);
这是您在不实现IXmlSerializable的情况下所能得到的最接近的结果(始终包含估算属性):
[XmlRoot("Val")]
public class DecimalField
{
[XmlText()]
public decimal Value { get; set; }
[XmlAttribute("Estimate")]
public bool Estimate { get; set; }
}
使用IXmlSerializable,您的类如下所示:
[XmlRoot("Val")]
public class DecimalField : IXmlSerializable
{
public decimal Value { get; set; }
public bool Estimate { get; set; }
public void WriteXml(XmlWriter writer)
{
if (Estimate == true)
{
writer.WriteAttributeString("Estimate", Estimate.ToString());
}
writer.WriteString(Value.ToString());
}
public void ReadXml(XmlReader reader)
{
if (reader.MoveToAttribute("Estimate") && reader.ReadAttributeValue())
{
Estimate = bool.Parse(reader.Value);
}
else
{
Estimate = false;
}
reader.MoveToElement();
Value = reader.ReadElementContentAsDecimal();
}
public XmlSchema GetSchema()
{
return null;
}
}
XmlSerializer xs = new XmlSerializer(typeof(DecimalField));
string serializedXml = null;
using (StringWriter sw = new StringWriter())
{
DecimalField df = new DecimalField() { Value = 12.0M, Estimate = false };
xs.Serialize(sw, df);
serializedXml = sw.ToString();
}
Console.WriteLine(serializedXml);
using (StringReader sr = new StringReader(serializedXml))
{
DecimalField df = (DecimalField)xs.Deserialize(sr);
Console.WriteLine(df.Estimate);
Console.WriteLine(df.Value);
}
您可以这样测试您的类:
[XmlRoot("Val")]
public class DecimalField : IXmlSerializable
{
public decimal Value { get; set; }
public bool Estimate { get; set; }
public void WriteXml(XmlWriter writer)
{
if (Estimate == true)
{
writer.WriteAttributeString("Estimate", Estimate.ToString());
}
writer.WriteString(Value.ToString());
}
public void ReadXml(XmlReader reader)
{
if (reader.MoveToAttribute("Estimate") && reader.ReadAttributeValue())
{
Estimate = bool.Parse(reader.Value);
}
else
{
Estimate = false;
}
reader.MoveToElement();
Value = reader.ReadElementContentAsDecimal();
}
public XmlSchema GetSchema()
{
return null;
}
}
XmlSerializer xs = new XmlSerializer(typeof(DecimalField));
string serializedXml = null;
using (StringWriter sw = new StringWriter())
{
DecimalField df = new DecimalField() { Value = 12.0M, Estimate = false };
xs.Serialize(sw, df);
serializedXml = sw.ToString();
}
Console.WriteLine(serializedXml);
using (StringReader sr = new StringReader(serializedXml))
{
DecimalField df = (DecimalField)xs.Deserialize(sr);
Console.WriteLine(df.Estimate);
Console.WriteLine(df.Value);
}
我认为使用一些属性可以实现这一点。但不确定属性是什么。