[英]Parsing XmlAttribute as complex type
我正在嘗試創建SVG格式的對象模型。 因此,因為它是XML,所以我正在使用XmlSerializer。 但是我有一個問題。 有一些名為“ style”的xml屬性。 它看起來像復雜類型,但表示為字符串。 有此屬性的示例:
風格= “填充:無;行程:#00FF00;筆划寬度:8.7489996;卒中miterLimit分別:4;中風不透明度:1;卒中dasharray:無;卒中dashoffset:8.74900006”
如您所見,有諸如“ fill”,“ stroke”,“ stroke-width”等屬性。我編寫了該類
public class SvgStyle
{
public string FillColor { get; set; }
public string StrokeColor { get; set; }
public float StrokeWidth { get; set; }
public int StrokeMiterlimit { get; set; }
public float StrokeOpacity { get; set; }
public float StrokeDashoffset { get; set; }
public string StrokeDasharray { get; set; }
}
和另一類
public abstract class SvgGraphicElement
{
[XmlAttribute("style")]
public SvgStyle Style { get; set; }
}
我所得到的只是例外
無法序列化Svg.SvgStyle類型的成員“ Style”。 XmlAttribute / XmlText不能用於編碼復雜類型。
我嘗試使用IXmlSerializable接口實現和OnSerializing / OnDeserializing方法,但是我得到的只是另一個例外。 有什么辦法可以將此字符串反序列化到我的班級嗎? 謝謝。
我相信您將需要在SVGStyle類上實現IXmlSerializable接口
http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx
只是讀到您已經嘗試過了; 抱歉。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace XmlTypes
{
public class SvgStyle : IXmlSerializable
{
private const string fillColorKey = "fill";
private const string strokeColorKey ="stroke";
private const string strokeWidthKey = "stroke-width";
private const string strokeMiterLimitKey = "stroke-miterlimit";
private const string strokeOpacityKey ="stroke-opacity";
private const string strokeDashArrayKey="stroke-dasharray";
private const string strokeDashOffsetKey = "stroke-dashoffset";
[XmlAttribute]
public String Style { get; set; }
public string FillColor { get; set; }
public string StrokeColor { get; set; }
public float StrokeWidth { get; set; }
public int StrokeMiterlimit { get; set; }
public float StrokeOpacity { get; set; }
public float StrokeDashoffset { get; set; }
public string StrokeDasharray { get; set; }
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
reader.MoveToContent();
Style = reader.GetAttribute("style");
Dictionary<string, string> lookupTable = ParseAttribute(Style);
FillColor = lookupTable[fillColorKey];
StrokeColor = lookupTable[strokeColorKey];
float strokeWidthFloatValue=0;
float.TryParse(lookupTable[strokeWidthKey] , out strokeWidthFloatValue) ;
StrokeWidth = strokeWidthFloatValue;
int strokeMiterLimitInt = 0;
Int32.TryParse(lookupTable[strokeMiterLimitKey] , out strokeMiterLimitInt);
StrokeMiterlimit = strokeMiterLimitInt;
int strokeOpacityInt = 0;
Int32.TryParse(lookupTable[strokeMiterLimitKey], out strokeOpacityInt);
StrokeOpacity = strokeOpacityInt;
int strokeDashOffsetInt = 0;
Int32.TryParse(lookupTable[strokeMiterLimitKey], out strokeDashOffsetInt);
StrokeDashoffset = strokeDashOffsetInt;
StrokeDasharray = lookupTable[strokeDashArrayKey];
Boolean isEmptyElement = reader.IsEmptyElement; // (1)
reader.ReadStartElement();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("style", Style);
}
private Dictionary<string, string> ParseAttribute(string attribute)
{
string[] arr = attribute.Split(';');
Dictionary<string, string> dic = new Dictionary<string, string>();
for(int i = 0; i < arr.Length; i++)
{
string[] arrItem = arr[i].Split(':');
dic.Add(arrItem[0], arrItem[1]);
}
return dic;
}
}
}
[TestMethod]
public void TestMethod1()
{
string xml = @"<SvgStyle style='fill:none;stroke:#00ff00;stroke-width:8.7489996;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:8.74900006'></SvgStyle>";
XmlSerializer x = new XmlSerializer(typeof(SvgStyle));
SvgStyle myTest = (SvgStyle)x.Deserialize(new StringReader(xml));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.