- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个由另一个应用程序提供的现有 XML 文件,看起来类似于:
<Root xmlns="http://someNameSpace">
<DisplayName>RootName</DisplayName>
<Groups>
<Group>
<Elements>
<a:anyType i:type="SomeType">
<DisplayName>ElementName</DisplayName>
</a>
</Elements>
</Group>
</Groups>
</Root>
我正在尝试编写一个可以用 XmlSerializer
反序列化的类,目前看起来像这样:
public class Root
{
public string DisplayName { get; set; }
public List<Group> Groups { get; set; }
}
public class Group : SomeType
{
public List<SomeType> Elements { get; set; }
}
public class SomeType
{
public string DisplayName { get; set; }
}
以及使用 XmlSerializer
的简单反序列化代码:
var serializer = new XmlSerializer(typeof (Root));
using (var streamReader = new StreamReader(somePathToXmlFile))
{
root = (Root) serializer.Deserialize(streamReader);
}
反序列化一切正常,除了 Elements
列表 - 它总是空的。我试过装饰 Elements
属性(property) Group
与 XmlAttributes
通过以下方式:
[XmlArrayItem(ElementName = "a")]
[XmlArrayItem(ElementName = "a:anyType")]
[XmlArrayItem(ElementName = "a", Type = typeof(SomeType))]
[XmlArrayItem(ElementName = "a:anyType", Type = typeof(SomeType))]
到目前为止,没有任何效果。
有谁知道获得 <a:anyType i:type="SomeType">
的正确方法吗?要反序列化到 Elements
中的项目名单?
编辑从那以后我意识到我遗漏了我的问题是 <Root>
实际上有一个命名空间:<Root xmlns="http://someNameSpace">
.请参阅上面的 XML。
最佳答案
考虑在根上添加命名空间声明的 xml
<Root xmlns:a="http://custom/a" xmlns:i="http://custom/i">
<DisplayName>RootName</DisplayName>
<Groups>
<Group>
<Elements>
<a:anyType i:type="SomeType">
<DisplayName>ElementName</DisplayName>
</a:anyType>
</Elements>
</Group>
</Groups>
</Root>
我已经使用以下 xml 属性成功反序列化它:
public class Group : SomeType
{
[XmlArrayItem(typeof(SomeType),ElementName = "anyType", Namespace = "http://custom/a")]
public List<SomeType> Elements { get; set; }
}
public class SomeType
{
[XmlElement(typeof(string),ElementName="DisplayName",Namespace="")]
public string DisplayName { get; set; }
}
或者,如果您不能将 namespace 声明添加到 xml,请使用 XmlNamespaceManager
var serializer = new XmlSerializer(typeof(Root));
var nameTable = new NameTable();
var namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("a", "http://custom/a");
namespaceManager.AddNamespace("i", "http://custom/i");
var parserContext = new XmlParserContext(null, namespaceManager, null, XmlSpace.None);
var settings = new XmlReaderSettings()
{
ConformanceLevel = ConformanceLevel.Fragment
};
using(var fileStream=File.OpenRead(somePathToXmlFile))
{
using(var reader=XmlReader.Create(fileStream,settings,parserContext))
{
var root = (Root)serializer.Deserialize(reader);
}
}
关于c# - 反序列化 :anyType 的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28533984/
您好,我正在编写这个程序,我正在尝试弄清楚如何比较数组中的两个项目以找到最大的项目。 public static > T getLargest(T [] a, int low, int high){
我正在使用答案 How do I create a new AnyType[] array? 中的代码。我的问题是如何初始化这个数组。当我尝试运行此代码时,我在 Clear() 上遇到空指针异常,我认
创建 UIBUttons 操作时,默认为 AnyType。为什么它是默认的,为什么它是一个选项?不应该只是 UIButton 吗? 最佳答案 我们可以将 任何 类型的 UIControl 连接到 IB
在这种情况下,最佳做法是什么?我想要一个与原始数组具有相同类型和长度的未初始化数组。 public static > void someFunction(AnyType[] someArray) {
我有一个由另一个应用程序提供的现有 XML 文件,看起来类似于: RootName
以下代码会导致转换异常,我不确定原因。 ParameterValueList 中的对象应该急切地解码为 JAXB 对象 ParameterValueStruct,但事实并非如此。一切都是直接从 soa
我正在尝试在 WCF 外部使用 DataContractSerializer 来序列化一个对象。本例中的对象继承自 CollectionBase 周围的旧通用包装器,例如 [KnownType(typ
尝试在 Swift 中制作并洗牌一副牌,该牌组由名为 cardDeck 的数组中的 Card 对象组成。然后我想洗牌。相关代码: var cardDeck = [card]() for
我的问题是我有一个 MessageList 类型的类,它包含 Message 项,定义如下: 我的消息类看起来像
我是编程界的新手,所以我只需要一些帮助。我必须在 android 上使用 web 服务。我已经做了一个工作正常的,但是当我必须传递一个参数时,问题就发生了。我不确定我做错了什么。 这是 AsyncTa
我有一个模式片段看起来像 hyperjaxb3 生成的
假设我有一个用户模式,它将“爱好”链接到它,作为一个数组字符串。 我最近从 MongoDB 切换到 SQL,所以,我的第一个想法是像 Mongo 一样接近它,并做出这样的东西; var userSch
xsd 在这里: http://www.iana.org/assignments/xml-registry/schema/epp-1.0.xsd 我不想修改 XSD(由第三方维护)或 XJC 生成的类
这是我的异步代码 @Override protected SoapObject doInBackground(String... params) { try { SoapObject requ
如您所知,.NET 框架中有一个通用类 List。我想用 C++ 编写一个通用类列表,我想在我的列表中存储指向一个类型的指针。这是类和测试程序的头文件和源代码: // header template
我有这个XML文件 和这个XSD 架构
我正在阅读有关 XML、XML-Schema、DTD 的资料,但我并不真正理解 xsd:any 和 xsd:anyType 之间的区别。 有人可以向我解释一下或指出一些好的文章吗? (请不要链接到 X
喜欢ISNULL函数中的参数吗? 我们可以用来定义的东西: create function getvalue ( @param1 varchar(max), @param2 varch
在使用 DataContractSerializer 时,我得到了很多我不想使用的 namespace 的额外属性。我在另一个项目中生成了一个类似的数据结构,没有所有额外的标记,所以我知道它是可能的。
我在我的 Android 应用程序中调用 Web 服务,方法是 getGramaNiladhariData(),我得到的结果是 SoapObject。 result = (SoapObject) en
我是一名优秀的程序员,十分优秀!