C# 正在尝试为我拥有的XML文件生成对象
我有一个XML文件,我需要从中提取值,并将它们放在另一个XML文件中 问题:C# 正在尝试为我拥有的XML文件生成对象,c#,xml,xsd,C#,Xml,Xsd,我有一个XML文件,我需要从中提取值,并将它们放在另一个XML文件中 问题: 另一个人正在为生成的XML文件创建“模式”。是否有人可以给我一些可以自动插入值的东西?我甚至需要从XML中提取任何东西,或者像XSLT这样的东西可以完成所有转换吗 下面的XML结构有问题吗?我尝试使用xsd2code生成对象,但当我使用LoadFromFileMethod时,不会加载任何对象-我读了一篇不太具体的文章,但是说“嵌套父对象”会导致XSD.exe和xsd2code出现问题 XLST是将xml从一种模式转换
XLST是将xml从一种模式转换为另一种模式的最佳方式。这正是它建造的目的。是一个优秀的XSLT教程。您真正需要的只是模式,或者他的一些xml示例来编写xslt文件
而且,我觉得您的xml格式很好 XLST是将xml从一种模式转换为另一种模式的最佳方式。这正是它建造的目的。是一个优秀的XSLT教程。您真正需要的只是模式,或者他的一些xml示例来编写xslt文件
而且,我觉得您的xml格式很好 如果您只想转换它,XSLT可能是解决方案,但是如果您需要对代码中的值执行任何操作,那么LINQ to Xml将使您的任务更容易。如果您只想转换它,XSLT可能是解决方案,但是,如果您需要对代码中的值执行任何操作,那么LINQ to Xml将使您的任务更容易。XSLT是Xml转换的工具
就您的XML而言,在许多应用程序中,您应该替换以下内容:
<Property name="Color">Red</Property>
红色
与:
红色
一些原因:
如果您想编写一个模式,以某种方式限制元素的内容(例如,限制为一个值列表),则元素必须通过其名称进行标识;不能为属性
属性等于“颜色”的属性
元素编写一个架构,也不能为属性
属性等于“大小”的元素编写另一个架构
如果元素名有意义,那么编写XPath谓词就更容易了。例如,Form[Color='Red']
比Form[Property[@name='Color'和.='Red']]更容易写(和读)
如果您以几乎相同的方式针对XML编写Linq查询,则上述情况也是正确的。将元素子代(“颜色”)
与元素子代(“属性”)。其中(x=>x.Attributes[“name”]==“Color”)
有些应用程序也适合使用通用命名的元素;上述论点并不确定。但是如果要这样做,应该有充分的理由。XSLT是XML转换的工具
就您的XML而言,在许多应用程序中,您应该替换以下内容:
<Property name="Color">Red</Property>
红色
与:
红色
一些原因:
如果您想编写一个模式,以某种方式限制元素的内容(例如,限制为一个值列表),则元素必须通过其名称进行标识;不能为属性
属性等于“颜色”的属性
元素编写一个架构,也不能为属性
属性等于“大小”的元素编写另一个架构
如果元素名有意义,那么编写XPath谓词就更容易了。例如,Form[Color='Red']
比Form[Property[@name='Color'和.='Red']]更容易写(和读)
如果您以几乎相同的方式针对XML编写Linq查询,则上述情况也是正确的。将元素子代(“颜色”)
与元素子代(“属性”)。其中(x=>x.Attributes[“name”]==“Color”)
有些应用程序也适合使用通用命名的元素;上述论点并不确定。但是如果你要这样做,你应该有充分的理由。我会使用XSLT,这里有一个小例子让你开始
将此示例代码复制到空的c#项目:
将上面的示例xml代码写入source.xml文件,并将以下xsl代码放入transform.xsl文件:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Section">
<OtherSection>
<xsl:apply-templates />
</OtherSection>
</xsl:template>
<xsl:template match="Form">
<OtherForm>
<xsl:attribute name="id">
<xsl:value-of select="@id" />
</xsl:attribute>
<xsl:apply-templates />
</OtherForm>
</xsl:template>
<xsl:template match="Control">
<OtherControl>
<!-- converts id attribute to an id tag -->
<id>
<xsl:value-of select="@id" />
</id>
<xsl:apply-templates />
</OtherControl>
</xsl:template>
<xsl:template match="Property">
<OtherProperty>
<!-- converts name attribute to an id attribute -->
<xsl:attribute name="id">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:value-of select="."/>
</OtherProperty>
</xsl:template>
</xsl:stylesheet>
由此产生的out.xml应该让您了解xsl是如何工作的,并有望让您开始工作
有关XSLT的更多信息,请查阅W3Schools上的。我将使用XSLT,下面是一个小示例,让您开始学习
将此示例代码复制到空的c#项目:
将上面的示例xml代码写入source.xml文件,并将以下xsl代码放入transform.xsl文件:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Section">
<OtherSection>
<xsl:apply-templates />
</OtherSection>
</xsl:template>
<xsl:template match="Form">
<OtherForm>
<xsl:attribute name="id">
<xsl:value-of select="@id" />
</xsl:attribute>
<xsl:apply-templates />
</OtherForm>
</xsl:template>
<xsl:template match="Control">
<OtherControl>
<!-- converts id attribute to an id tag -->
<id>
<xsl:value-of select="@id" />
</id>
<xsl:apply-templates />
</OtherControl>
</xsl:template>
<xsl:template match="Property">
<OtherProperty>
<!-- converts name attribute to an id attribute -->
<xsl:attribute name="id">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:value-of select="."/>
</OtherProperty>
</xsl:template>
</xsl:stylesheet>
由此产生的out.xml应该让您了解xsl是如何工作的,并有望让您开始工作
有关XSLT的更多信息,请查阅W3Schools上的
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Section">
<OtherSection>
<xsl:apply-templates />
</OtherSection>
</xsl:template>
<xsl:template match="Form">
<OtherForm>
<xsl:attribute name="id">
<xsl:value-of select="@id" />
</xsl:attribute>
<xsl:apply-templates />
</OtherForm>
</xsl:template>
<xsl:template match="Control">
<OtherControl>
<!-- converts id attribute to an id tag -->
<id>
<xsl:value-of select="@id" />
</id>
<xsl:apply-templates />
</OtherControl>
</xsl:template>
<xsl:template match="Property">
<OtherProperty>
<!-- converts name attribute to an id attribute -->
<xsl:attribute name="id">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:value-of select="."/>
</OtherProperty>
</xsl:template>
</xsl:stylesheet>