C#将Xml属性反序列化为值,而不使用类包装它
我试图在下面反序列化xml:C#将Xml属性反序列化为值,而不使用类包装它,c#,xml-deserialization,xml-attribute,C#,Xml Deserialization,Xml Attribute,我试图在下面反序列化xml: <venue href="http://SomeUrl"> <location id="ABC"/> <title>Some title</title> </venue> 但在我看来,将简单字符串从位置包装到单独的类中是一个相当乏味的解决方案。我想要实现的是创建更简单的展平模型,如下所示: [XmlRoot(ElementName = "venue")] public class VenueM
<venue href="http://SomeUrl">
<location id="ABC"/>
<title>Some title</title>
</venue>
但在我看来,将简单字符串从位置
包装到单独的类中是一个相当乏味的解决方案。我想要实现的是创建更简单的展平模型,如下所示:
[XmlRoot(ElementName = "venue")]
public class VenueModel2
{
[SomeMagicAttribute]
public string LocationId;
[XmlElement("title")]
public string Title;
[XmlAttribute("href")]
public string Href;
}
那么第一个问题呢?是否可以使用C#
System.Xml.Serialization
?如果是,那么获取这些数据的神奇属性是什么?我发现首先需要将xsl转换(如下所示)应用于输入xml
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:template match="/">
<venue>
<href><xsl:value-of select="/venue/@href" /></href>
<locationId><xsl:value-of select="/venue/location/@id" /></locationId>
<title><xsl:value-of select="/venue/title" /></title>
</venue>
</xsl:template>
</xsl:stylesheet>
然后输出xml如下所示:
<venue>
<href>SomeUrl</href>
<locationId>ABC</locationId>
<title>Some title</title>
</venue>
某物
基础知识
一些头衔
它可以反序列化为我想要的形式。这里描述了C代码中的这种翻译:我发现首先我需要将xsl翻译(如下所示)应用于输入xml
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:template match="/">
<venue>
<href><xsl:value-of select="/venue/@href" /></href>
<locationId><xsl:value-of select="/venue/location/@id" /></locationId>
<title><xsl:value-of select="/venue/title" /></title>
</venue>
</xsl:template>
</xsl:stylesheet>
然后输出xml如下所示:
<venue>
<href>SomeUrl</href>
<locationId>ABC</locationId>
<title>Some title</title>
</venue>
某物
基础知识
一些头衔
它可以反序列化为我想要的形式。这里描述了C#代码中的这个翻译:在类位置中缺少文本:[XmlText]公共字符串文本{get;set;},否则无法读取value.location中的值。location没有任何文本值。它只包含ID send as属性。这是它设计的唯一目的,在这种情况下,它不应该是属性,而应该是locationid的值。但我想你不能改变这个?是的。我知道它很枯燥,但这就是它如何从外部API恢复过来的。我不想在我的解决方案中使用同样枯燥的代码。但是我不想给数据模型eAuther添加任何逻辑,所以我不想添加传统的getter/setter来获取这是因为VenueModel属性在类位置中缺少文本:[XmlText]公共字符串文本{get;set;},否则无法读取value.location中的值。location没有任何文本值。它只包含ID send as属性。这是它设计的唯一目的,在这种情况下,它不应该是属性,而应该是locationid的值。但我想你不能改变这个?是的。我知道它很枯燥,但这就是它如何从外部API恢复过来的。我不想在我的解决方案中使用同样枯燥的代码。但是我不想给数据模型eAuther添加任何逻辑,所以我不想添加传统的getter/setter来获得这是作为VenueModel属性的