如何在单个 class 属性 中读取不同的 XML 元素?
How to read different XML Elements in single class property?
我有一个 xsd.exe
输出 c# class:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class OrdinateZacrep
{
private decimal xField;
private decimal yField;
/// <remarks/>
public decimal x
{
get
{
return this.xField;
}
set
{
this.xField = value;
}
}
/// <remarks/>
public decimal y
{
get
{
return this.yField;
}
set
{
this.yField = value;
}
}
}
和XML简单:
<ordinate>
<x>587865.52</x>
<y>1397457.84</y>
</ordinate>
但有时 XML 看起来像:
<ordinate>
<x>587865.52</x>
<_y>1397457.84</_y>
</ordinate>
我怎样才能用单曲阅读这个简单的class?
这里是 xsd 切片:
<xsd:complexType name="OrdinateOut">
<xsd:annotation>
<xsd:documentation>Parcel coordinates</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="x" type="xsd:decimal">
<xsd:annotation>
<xsd:documentation>Ordinate X</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="y" type="xsd:decimal">
<xsd:annotation>
<xsd:documentation>Ordinate Y</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
一个可能的解决方案是应用 xslt 来转换 xml 文件,而不是根据您的架构作为预处理步骤。应用于已正确格式化的 xml 个文件应该是安全的。
例如,此 xslt 将所有 ordinate/_y
元素重命名为 ordinate/y
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="ordinate/_y">
<y><xsl:apply-templates select="@*|node()" /></y>
</xsl:template>
</xsl:stylesheet>
您可以在此 page 上进行测试,并且有关于如何在 C# 中使用 XSLT 的示例,只是 google :)
编辑:我忘记了蛮力方式:在解析文件之前将字符串替换 <_y>
和 </_y>
。我不推荐这样做,但这可能是一个解决方案。
您正在使用私有变量 xField 和 yField。所以只需创建一个新的 属性 并像这样保存到 xField 和 yField
public decimal _y
{
get
{
return this.yField;
}
set
{
this.yField = value;
}
}
我有一个 xsd.exe
输出 c# class:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class OrdinateZacrep
{
private decimal xField;
private decimal yField;
/// <remarks/>
public decimal x
{
get
{
return this.xField;
}
set
{
this.xField = value;
}
}
/// <remarks/>
public decimal y
{
get
{
return this.yField;
}
set
{
this.yField = value;
}
}
}
和XML简单:
<ordinate>
<x>587865.52</x>
<y>1397457.84</y>
</ordinate>
但有时 XML 看起来像:
<ordinate>
<x>587865.52</x>
<_y>1397457.84</_y>
</ordinate>
我怎样才能用单曲阅读这个简单的class?
这里是 xsd 切片:
<xsd:complexType name="OrdinateOut">
<xsd:annotation>
<xsd:documentation>Parcel coordinates</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="x" type="xsd:decimal">
<xsd:annotation>
<xsd:documentation>Ordinate X</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="y" type="xsd:decimal">
<xsd:annotation>
<xsd:documentation>Ordinate Y</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
一个可能的解决方案是应用 xslt 来转换 xml 文件,而不是根据您的架构作为预处理步骤。应用于已正确格式化的 xml 个文件应该是安全的。
例如,此 xslt 将所有 ordinate/_y
元素重命名为 ordinate/y
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="ordinate/_y">
<y><xsl:apply-templates select="@*|node()" /></y>
</xsl:template>
</xsl:stylesheet>
您可以在此 page 上进行测试,并且有关于如何在 C# 中使用 XSLT 的示例,只是 google :)
编辑:我忘记了蛮力方式:在解析文件之前将字符串替换 <_y>
和 </_y>
。我不推荐这样做,但这可能是一个解决方案。
您正在使用私有变量 xField 和 yField。所以只需创建一个新的 属性 并像这样保存到 xField 和 yField
public decimal _y
{
get
{
return this.yField;
}
set
{
this.yField = value;
}
}