在XML / XSD模式文件中,我们如何从元素中提取(解析)某些信息?

问题描述 投票:0回答:1

是否有一种方法可以解析XML文件中的某些数据,然后将该信息输出到excel(csv)文件中?我想知道是否可以在C#应用程序中将其自动化,而不是手动浏览XML文件并在excel工作表中对其进行更新。这样,如果我有很多XML文件,就可以运行该应用程序以获取所需的信息。

例如,这是下面的XML文件中的元素之一:

<element type="xpowercomponent_FeaturedTiles" UID="40204fc9b5424b349e03134d777d29bc" label="Featured Tiles" readonly="false" hidden="false" default="" required="false" Component="" CompTypes="ThemedTileSetComponent;ThemedTileSetElectedComponent;" AutoEmbed="" WrappedUp="" AllowWrappingChange="" />

我试图从XML的每个元素中提取的数据是,元素:类型,标签,CompTypes,只读,隐藏,必需。

我已经尝试执行以下操作,但是由于我不太熟悉XML,因此不确定这种方法是否可行:

    static void Main(string[] args)
        {
            XmlSchemaSet schemas = new XmlSchemaSet();
            schemas.Add("", XmlReader.Create(new StringReader(File.ReadAllText("schemapage.xml"))));
            schemas.Compile();
            AnalyseSchema(schemas);
            Console.ReadLine();
        }
    }

XmlSchema customerSchema = null;
        foreach (XmlSchema schema in set.Schemas())
        {
            customerSchema = schema;
        }

这里是完整的XML文件:

<Schema xmlns:dt="urn:schemas-microsoft-com:datatypes" name="TopTier" xmlns="urn:schemas-microsoft-com:xml-data">
      <AttributeType name="ID" dt:type="id" required="yes" />
      <AttributeType name="Name" dt:type="string" required="yes" />
      <AttributeType name="UniqueID" dt:type="int" default="schemas/107" />
      <AttributeType name="Icon" dt:type="int" default="2" />
      <AttributeType name="FriendlyName" dt:type="string" default="Top Tier" />
      <AttributeType name="SS" dt:type="boolean" default="true" />
      <AttributeType name="DSS" dt:type="boolean" default="true" />
      <AttributeType name="ViewName" dt:type="" default="" />
      <AttributeType name="DefaultCategorization" dt:type="string" default="" />
      <AttributeType name="Version" dt:type="int" default="9" />
      <ElementType name="Exports" content="textOnly" />
      <ElementType name="Title" dt:type="string" content="textOnly" />
      <ElementType name="Excerpt" dt:type="string" content="textOnly">
        <description>This info is used to display on pages that link to this page.</description>
      </ElementType>
      <ElementType name="ThumbnailImage" dt:type="imagefile" content="textOnly">
        <description>Thumbnail dimension should be 75 x 75 pixels.</description>
      </ElementType>
      <ElementType name="xhtml_BodyCopy" content="textOnly" />
      <ElementType name="xpowerlist_LeftSidebarComponentsOverride" content="eltOnly" />
      <ElementType name="xpowercomponent_LeftSidebarComponent" content="eltOnly" />
      <ElementType name="xpowercomponent_FeaturedTiles" content="eltOnly" />
      <ElementType name="SecondaryTitle" dt:type="string" content="textOnly" />
      <ElementType name="xpowerlist_SecondaryComponentList" content="eltOnly" />
      <ElementType name="xpowercomponent_SecondaryItem" content="eltOnly" />
      <ElementType name="xpowergroupstart_SEOGroup" content="eltOnly">
        <description>Search Engine Optimization Group</description>
      </ElementType>
      <ElementType name="BrowserTitle" dt:type="string" content="textOnly" />
      <ElementType name="MetaDescription" dt:type="string" content="textOnly" />
      <ElementType name="NoIndex" dt:type="boolean" content="textOnly" />
      <ElementType name="NoFollow" dt:type="boolean" content="textOnly" />
      <ElementType name="NoODP" dt:type="boolean" content="textOnly" />
      <ElementType name="Priority" dt:type="enumeration" values="0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0" content="textOnly" />
      <ElementType name="PublishDate" dt:type="localdate" content="textOnly" />
      <ElementType name="xpowergroupend_" content="eltOnly" />
      <ElementType name="xpowernavigation_BreadcrumbNavigation" content="eltOnly" />
      <ElementType name="xpowercomponent_SectionControl" content="eltOnly" />
      <ElementType name="TopTier" content="eltOnly" order="seq">
        <attribute type="ID" />
        <attribute type="Name" />
        <element type="Title" UID="2bbcb2816ba743e2bd935d526f47d688" label="Title" readonly="false" hidden="false" default="" required="true" />
        <element type="Excerpt" UID="2ac3ade7d9c34cae8a7f304d9754db86" label="Excerpt" readonly="false" hidden="false" default="" required="true" />
        <element type="ThumbnailImage" UID="f4940dbfe27f4783a97c356f3ba0f959" label="Thumbnail Image" readonly="false" hidden="false" default="" required="false" Expanded="false" Width="" Height="" Border="" HSpace="" VSpace="" Alignment="" AlternateText="" />
        <element type="xhtml_BodyCopy" UID="055d4709ffb24402a14f9d13af417401" label="Body Copy" readonly="false" hidden="false" default="" required="false" Height="" />
        <element type="xpowerlist_LeftSidebarComponentsOverride" UID="1d281fa780b6419089c52913640ade93" label="Left Sidebar Components Override" readonly="false" hidden="false" default="" required="false" Enclosed="" AllowEnclosureChange="" List="xpowercomponent_LeftSidebarComponent" Component="" CompTypes="" AutoEmbed="" WrappedUp="" AllowWrappingChange="" />
        <element type="xpowercomponent_FeaturedTiles" UID="40204fc9b5424b349e03134d777d29bc" label="Featured Tiles" readonly="false" hidden="false" default="" required="false" Component="" CompTypes="ThemedTileSetComponent;ThemedTileSetElectedComponent;" AutoEmbed="" WrappedUp="" AllowWrappingChange="" />
        <element type="SecondaryTitle" UID="3d00fbe341ff4f2481c6199deb046997" label="Secondary Title" readonly="false" hidden="false" default="" required="false" />
        <element type="xpowerlist_SecondaryComponentList" UID="3bc6293a826a4f2081dca1c5e1415978" label="Secondary Component List" readonly="false" hidden="false" default="" required="false" Enclosed="" AllowEnclosureChange="" List="xpowercomponent_SecondaryItem" Component="" CompTypes="TaxonomyTileComponent;ServiceComponent;ContactComponent;TitledCollection" AutoEmbed="" WrappedUp="" AllowWrappingChange="" />
        <element type="xpowergroupstart_SEOGroup" UID="bf312bbd47d74c758d13695b8fa011c0" label="SEO Group" readonly="false" hidden="true" default="" required="false" Enclosed="" AllowEnclosureChange="" />
        <element type="BrowserTitle" UID="c777f36e6f094b50a04692d3bfef387e" label="Browser Title" readonly="false" hidden="false" default="" required="false" />
        <element type="MetaDescription" UID="05bab416415541beb05e90b94f7e5248" label="Meta Description" readonly="false" hidden="false" default="" required="false" />
        <element type="NoIndex" UID="e224c1a3aaa846d9b19579d59d9b1af7" label="No Index" readonly="false" hidden="false" default="" required="false" />
        <element type="NoFollow" UID="b40ae068c0d749c4bb8ecd589388bb1b" label="No Follow" readonly="false" hidden="false" default="" required="false" />
        <element type="NoODP" UID="2557027d711d41c3b3ccbd3aab4650dd" label="No ODP" readonly="false" hidden="false" default="" required="false" />
        <element type="Priority" UID="f49eec4e94864e03aedbd0e2be6a2822" label="Priority" readonly="false" hidden="false" default="" required="false" values="0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0" />
        <element type="PublishDate" UID="5fdba65915744addb710631476822112" label="Publish Date" readonly="true" hidden="true" default="" required="false" />
        <element type="xpowergroupend_" />
        <element type="xpowernavigation_BreadcrumbNavigation" UID="140b1582e50e4a098e190942980bd728" label="Breadcrumb Navigation" readonly="false" hidden="true" default="" required="true" Expanded="false" Navigation="Ancestors" GenerationOrder="down" StartPage="x5" MaxNodes="" MaxDepth="" Query="">
          <element type="Exports" default="" />
        </element>
        <element type="xpowercomponent_SectionControl" UID="56564fa9c2bd4e9ba44a417960146376" label="SectionControl" readonly="false" hidden="true" default="" required="true" Component="" CompTypes="SectionControl" AutoEmbed="" WrappedUp="" AllowWrappingChange="" />
      </ElementType>
    </Schema>
c# xml linq xsd xml-parsing
1个回答
0
投票

您可以使用XSLT

XSLT是基于XML的模板语言,它定义了从XML到所需输出的转换,在您的情况下为CSV。

对于您的输入,XSLT模板将类似于:

<xsl: template match="/*[@name='whatever']"><xsl:value-of select="."/>,</xsl:template>

即。在文档元素下查找具有名为“名称”的特定值的属性的所有元素,并在该元素的值后加上逗号。对于一行的预期最后值,您将改为输出换行符。

然后解析该模板:

var transform = new XslCompiledTranform();transform.Load(path to my template);

并在输入上运行:

transform.Transform(path to input, path to output);

如果正确构建了转换,则输出文件是格式正确的CSV文件。

请参见documentation for XslCompiledTranform in .net

© www.soinside.com 2019 - 2024. All rights reserved.