尝试为我拥有的XML文件生成对象

时间:2011-03-24 17:26:23

标签: c# xml xsd

我有一个XML文件,我需要从中提取值,并将它们放在另一个XML文件中。

问题:

  1. 另一个人正在为生成的XML文件创建“模式”。是否有人可以给我的东西会自动插入值?我是否甚至需要从XML中提取任何内容,或者像XSLT这样的内容可以进行所有转换?

  2. 下面的XML结构是否存在问题?我尝试使用xsd2code生成对象但是当我使用LoadFromFileMethod时没有任何内容会加载 - 我读了一篇不太具体的文章,但是说“嵌套父代”会导致XSD.exe和xsd2code出现问题。

  3. <Section>
    <Form id="1"...>
    <Control id="12523"..> <--Some have this some don't
      <Property name="Color">Red</Property>
      <Property name="Size">Large</Property>
    </Control>
    </Form>
    <Form id="2"...>
      <Property name="Color">Blue</Property>
      <Property name="Size">Large</Property>
    </Form>
    <Form id="3"...>
      <Property name="Color">Red</Property>
      <Property name="Size">Small</Property>
    </Form>
    </Section>
    

    感谢您的指导!

4 个答案:

答案 0 :(得分:2)

XSLT是XML转换的工具。

就你的XML而言,在很多应用程序中你应该替换它:

<Property name="Color">Red</Property>

使用:

<Color>Red</Color>

一些原因:

  1. 如果要编写以某种方式限制元素内容的模式(例如,对某个值列表之一),则该元素必须可以通过其名称进行标识;您不能为Property元素编写一个模式,其中name属性等于“颜色”,而Property元素的另一个模式name属性等于“大小”。

  2. 如果元素名称有意义,则编写XPath谓词会更容易。例如,Form[Color = 'Red']Form[Property[@name='Color' and .='Red']]

  3. 更容易编写(和阅读)
  4. 如果您以非常相同的方式编写针对XML的Linq查询,则上述情况也是如此。将Element.Descendants("Color")Element.Descendents("Property").Where(x => x.Attributes["name"] == "Color")进行比较。

  5. 有些应用程序也适用于通用名称元素;上述论点不是确定的。但如果你打算这样做,你应该有充分的理由。

答案 1 :(得分:1)

XLST是将xml从一个模式转换为另一个模式的最佳方法。这正是它的目的。 http://w3schools.com/xsl/default.asp是一个优秀的XSLT教程。你真正需要的只是架构,或者用于编写xslt文件的xml的几个例子。

另外,你的xml看起来很好/格式很好。

答案 2 :(得分:1)

如果您只想转换它,XSLT可能是解决方案,但如果您需要对代码中的值执行任何操作,那么LINQ to Xml将使您的任务更加轻松。

答案 3 :(得分:1)

我会使用XSLT,这是一个让你入门的小例子。

将此示例代码复制到空的c#项目中:

static void Main(string[] args) {
    const string xmlPath = "source.xml";
    const string xslPath = "transform.xsl";
    const string outPath = "out.xml";

    try {
        //load the Xml doc
        var xmlDoc = new XPathDocument(xmlPath);

        //load the Xsl 
        var xslDoc = new XslCompiledTransform();
        xslDoc.Load(xslPath);

        // create the output file
        using (var outDoc = new XmlTextWriter(outPath, null)) {
            //do the actual transform of Xml
            xslDoc.Transform(xmlDoc, null, outDoc);
        }
    }
    catch (Exception e) {
        Console.WriteLine("Exception: {0}", e.ToString());
    }
}

将上面的示例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上查找tutorial

相关问题