gpt4 book ai didi

在元素名称和属性中使用多个命名空间的 C# XML 反序列化

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:59 28 4
gpt4 key购买 nike

我正在尝试读入这段 xml 代码,并且大部分内容都是正确的。我遇到的问题是围绕“元素”元素和具有类型属性的命名空间。

我正在尝试读取的 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 &amp; 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 16

最佳答案

你需要改变你的模型来提供一些东西来反序列化 archimate:Principle 类型。此外,您需要更正 Element 上 Type 属性的命名空间。

根据您的 XML,我假设 Element 是一个基类,并且 type 属性指示要使用的父类(super class)型,我已将其放入新类中称为原则。如果你不想要一个单独的类,你可以只使用 Element,但是你需要给它 XmlType 属性来告诉 Serializer 在看到 xsi:type="archimate:Principle" 时使用它。

[XmlType("Principle", Namespace = "http://www.archimatetool.com/archimate")]
public class Principle : Element
{
[XmlAttribute("name")]
public string Name { get; set; }

[XmlAttribute("id")]
public string Id { get; set; }
}

[XmlRoot(ElementName = "element", Namespace = "http://www.archimatetool.com/archimate")]
[XmlInclude(typeof(Principle))]
public class Element
{
[XmlAttribute(AttributeName = "type", Form = XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Type { get; set; }
}

关于在元素名称和属性中使用多个命名空间的 C# XML 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51406387/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com