c# - 在 C# 中反序列化 XML 数据时出错
问题描述
我有一个 XML 格式的 SVG 文件,我正在尝试将 XML 反序列化为 C# 对象,但是在尝试反序列化 XML 数据时出现错误
下面是我的代码
XmlSerializer serializer = new XmlSerializer(typeof(List<rootobject>));
object objectList = null;
using (StreamReader stream = new StreamReader(@"C:\Users\sana-user\Documents\Visual Studio
2015\Projects\SVGXMLToJsonApp\SVGXMLToJsonApp\File\3rect.svg"))
{
objectList = serializer.Deserialize(stream);
}
Console.WriteLine(objectList);
下面是我的课
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2000/svg")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.w3.org/2000/svg", IsNullable = false)]
public partial class svg
{
private svgStyle styleField;
private svgG[] gField;
private string[] textField;
private decimal versionField;
private string idField;
private string xField;
private string yField;
private string viewBoxField;
private string style1Field;
private string spaceField;
public svgStyle style
{
get
{
return this.styleField;
}
set
{
this.styleField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("g")]
public svgG[] g
{
get
{
return this.gField;
}
set
{
this.gField = value;
}
}
[System.Xml.Serialization.XmlTextAttribute()]
public string[] Text
{
get
{
return this.textField;
}
set
{
this.textField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal version
{
get
{
return this.versionField;
}
set
{
this.versionField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string x
{
get
{
return this.xField;
}
set
{
this.xField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string y
{
get
{
return this.yField;
}
set
{
this.yField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string viewBox
{
get
{
return this.viewBoxField;
}
set
{
this.viewBoxField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute("style")]
public string style1
{
get
{
return this.style1Field;
}
set
{
this.style1Field = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/XML/1998/namespace")]
public string space
{
get
{
return this.spaceField;
}
set
{
this.spaceField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2000/svg")]
public partial class svgStyle
{
private string typeField;
private string valueField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2000/svg")]
public partial class svgG
{
private svgGText textField;
private svgGRect rectField;
private string[] textField1;
private string idField;
public svgGText text
{
get
{
return this.textField;
}
set
{
this.textField = value;
}
}
public svgGRect rect
{
get
{
return this.rectField;
}
set
{
this.rectField = value;
}
}
[System.Xml.Serialization.XmlTextAttribute()]
public string[] Text
{
get
{
return this.textField1;
}
set
{
this.textField1 = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2000/svg")]
public partial class svgGText
{
private string idField;
private string transformField;
private string classField;
private string valueField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string transform
{
get
{
return this.transformField;
}
set
{
this.transformField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string @class
{
get
{
return this.classField;
}
set
{
this.classField = value;
}
}
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2000/svg")]
public partial class svgGRect
{
private string idField;
private decimal xField;
private decimal yField;
private string classField;
private decimal widthField;
private decimal heightField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal x
{
get
{
return this.xField;
}
set
{
this.xField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal y
{
get
{
return this.yField;
}
set
{
this.yField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public string @class
{
get
{
return this.classField;
}
set
{
this.classField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal width
{
get
{
return this.widthField;
}
set
{
this.widthField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal height
{
get
{
return this.heightField;
}
set
{
this.heightField = value;
}
}
}
错误即将上线objectList = serializer.Deserialize(stream);
错误是 {"http://www.w3.org/2000/svg'> 不是预期的。"}
下面是我的 XML 数据
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 22.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="_x30_" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 1366 768" style="enable-background:new 0 0 1366 768;" xml:space="preserve">
<style type="text/css">
.st0{fill:none;stroke:#000000;stroke-width:0.7087;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;}
.st1{fill:#105689;}
.st2{font-family:'ArialMT';}
.st3{font-size:56.5499px;}
.st4{fill:#4554A5;}
.st5{font-size:56.5574px;}
.st6{fill:#2776BB;}
</style>
<g id="LWPOLYLINE">
<rect id="idrect3" x="251.4" y="-0.3" class="st0" width="866" height="300.2"/>
</g>
<g id="LWPOLYLINE_1_">
<rect id="idrect2" x="248.6" y="366.5" class="st0" width="500.3" height="400.2"/>
</g>
<g id="LWPOLYLINE_2_">
<rect id="idrect1" x="811.4" y="364.2" class="st0" width="300.2" height="404.1"/>
</g>
<g id="TEXT">
<text id="idnano" transform="matrix(1 0 0 1 515.7997 166.1773)" class="st1 st2 st3">Nano Tech</text>
</g>
<g id="TEXT_1_">
<text id="idmigalo" transform="matrix(1 0 0 1 420.2463 553.5321)" class="st4 st2 st5">Migalo</text>
</g>
<g id="TEXT_2_">
<text id="idprime" transform="matrix(1 0 0 1 883.9615 567.5667)" class="st6 st2 st5">Prime</text>
</g>
</svg>
尝试了很多东西,但我不明白为什么会出现这个错误。谁能帮我解决这个错误。我被这个错误困扰了一周,无法解决它。
解决方案
现在这就是我解决这个问题的方法。
当然,您必须添加错误处理和空值检查。但这就像我认为你不会很快想到的那样简单。
private void ConvertXmlToClass()
{
try
{
XDocument doc = XDocument.Load(@"c:\temp\del.xml");
string jsonText = JsonConvert.SerializeXNode(doc);
dynamic dyn = JsonConvert.DeserializeObject<ExpandoObject>(jsonText);
dynamic svg = dyn.svg;
var result = new Svg
{
g = new List<G>(),
svgStyle = GetString(svg, "@style"),
id = GetString(svg, "@id"),
x = GetString(svg, "@x"),
y = GetString(svg, "@y"),
viewBox = GetString(svg, "@viewBox"),
cssStyle = GetString(((IDictionary<string, object>)svg)["style"] as ExpandoObject, "#text"),
};
foreach (ExpandoObject gObj in svg.g)
{
var g = new G
{
Id = GetString(gObj, "@id"),
};
if (PropertyExist(gObj, "rect"))
{
var rect = ((IDictionary<string, object>)gObj)["rect"] as ExpandoObject;
g.Rect = new Rect
{
Class = GetString(rect, "@class"),
id = GetString(rect, "@id"),
height = GetString(rect, "@height"),
width = GetString(rect, "@width"),
x = GetString(rect, "@x"),
y = GetString(rect, "@y"),
};
}
if (PropertyExist(gObj, "text"))
{
var txt = ((IDictionary<string, object>)gObj)["text"] as ExpandoObject;
g.Text = new Text
{
Class = GetString(txt, "@class"),
id = GetString(txt, "@id"),
TextData = GetString(txt, "#text"),
transform = GetString(txt, "@transform"),
};
}
result.g.Add(g);
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
private string GetString(ExpandoObject obj, string key)
{
return ((IDictionary<string, object>)obj)[key] as string;
}
private bool PropertyExist(dynamic settings, string name)
{
if (settings is ExpandoObject)
return ((IDictionary<string, object>)settings).ContainsKey(name);
return settings.GetType().GetProperty(name) != null;
}
private class Svg
{
public string id { get; set; }
public string x { get; set; }
public string y { get; set; }
public string viewBox { get; set; }
public string cssStyle { get; set; }
public string svgStyle { get; set; }
public List<G> g { get; set; }
}
private class G
{
public string Id { get; set; }
public Rect Rect { get; set; }
public Text Text { get; set; }
}
private class Text
{
public string id { get; set; }
public string transform { get; set; }
public string Class { get; set; }
public string TextData { get; set; }
}
private class Rect
{
public string id { get; set; }
public string x { get; set; }
public string y { get; set; }
public string Class { get; set; }
public string width { get; set; }
public string height { get; set; }
}
[编辑]
现在对这段代码进行一些解释。
首先,我们将 xml 读取为 LINQ to XML 类的 XDocument,然后使用 JsonConvert 将其转换为 Dynamic 及其 ExpandoObject。
所以现在你有了这个不错的 ExpandoObject,在这里我们使用它是一个键和值的字典。我们在 GetString 函数中使用它以字符串形式返回 ExpandoObject 值。
推荐阅读
- ios - 关于在 Swift 中显示 viewcontroller 时标签栏不可见的问题
- android - 如何知道应用程序中的 *page* 是否使用 React Native(或纯原生)?
- build - Nix:编写一个使用 waf 而不是 make 的包
- javascript - 按钮图片无法点击
- android-studio - 我的模拟器显示了一个更大的状态栏,而不是设计部分中显示的状态栏。我是 android studio 的新手
- javascript - 如何阻止我的网格超出其容器 div?
- php - image-url 中的空格问题
- python - 尝试计算然后显示函数的梯度向量
- php - 是否可以在 GCP 的 Google Compute Engine 中强制使用较低的 PHP 版本?
- apache-kafka-streams - 由于抑制功能而怀疑内存不足问题