我正在尝试读入这段xml代码,大多数情况下它都是正确的。我遇到的问题是关于" element“元素和带有type属性的命名空间。
我正在尝试读入的XML文件(text.xml):
<?xml version="1.0" encoding="UTF-8"?>
<archimate:model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:archimate="http://www.archimatetool.com/archimate" name="ACME"
id="38f940a6-9fc7-4619-9806-fd4d48397af7" version="4.0.0">
<folder name="Strategy" id="ffc905fd-a78c-4311-b2f6-a188c00ed10a" type="strategy"/>
<folder name="Business" id="0d806081-438f-4ae5-86d9-8ff5ee4e9f1a" type="business"/>
<folder name="Application" id="3566e95c-c070-46bb-bde3-f6017ae49dc1" type="application"/>
<folder name="Technology & Physical" id="4fabc4fa-a882-4843-ae69-170b66df7685" type="technology"/>
<folder name="Motivation" id="ce5e0874-1c06-41c1-9b95-eec6558afa89" type="motivation">
<element xsi:type="archimate:Principle" name="Secure the Whole" id="9546e727-f9f7-402a-a4a2-50519d697d75"/>
</folder>
</archimate:model>为XML输入和输出标记我的模型的代码:
using System;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Collections.Generic;
namespace archimate_reporter.Models
{
public class Folder
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlElement(ElementName = "element")]
public List<Element> Element { get; set; }
}
[XmlRoot(ElementName="element")]
public class Element
{
[XmlAttribute(AttributeName = "type", Form=XmlSchemaForm.Qualified, Namespace="http://www.archimatetool.com/archimate")]
public string Type { get; set; }
}
[XmlRoot(ElementName = "model", Namespace = "http://www.archimatetool.com/archimate")]
public class Model
{
[XmlElement(ElementName = "folder", Namespace = "")]
public List<Folder> Folder { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "archimate", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Archimate { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
}
}调用测试程序:
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using archimate_reporter.Models;
namespace archimate_reporter
{
class Program
{
static void Main(string[] args)
{
Program t = new Program();
t.DeserializeObject("resource//test.xml");
}
private void DeserializeObject(string filename)
{
XmlSerializer serializer = new
XmlSerializer(typeof(Model));
FileStream fs = new FileStream(filename, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
Model i;
i = (Model)serializer.Deserialize(reader);
fs.Close();
}
}
}程序抛出的堆栈错误:
Unhandled Exception: System.InvalidOperationException: There is an error in XML document (11, 6). ---> System.InvalidOperationException: The specified type was not recognized: name='Principle', namespace='http://www.archimatetool.com/archimate', at <element xmlns=''>.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderModel.Read2_Element(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderModel.Read3_Folder(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderModel.Read4_Model(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderModel.Read5_model()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at archimate_reporter.Program.DeserializeObject(String filename) in D:\mmcke\workspace\archimate-reporter\Program.cs:line 36
at archimate_reporter.Program.Main(String[] args) in D:\mmcke\workspace\archimate-reporter\Program.cs:line 16https://stackoverflow.com/questions/51406387
复制相似问题