将Xml文件拆分为多个部分

时间:2019-01-19 13:23:19

标签: c# xml-parsing

我想将以下xml文件拆分为4个单独的部分(我需要放置链接,因为它的发布时间太长了:

https://www.dropbox.com/s/dzqyab0wewpuf5t/1547917556__0__qti_166292.xml?dl=0

我想了解以下部分: 第1部分:




<!DOCTYPE questestinterop SYSTEM "ims_qtiasiv1p2p1.dtd">

<!--Generated by ILIAS XmlWriter-->

<questestinterop>


<item title="titel" maxattempts="0" ident="il_0_qst_152800">

<qticomment>beschreibung</qticomment>

<duration>P0Y0M0DT0H1M0S</duration>


<itemmetadata>


<qtimetadata>


<qtimetadatafield>

<fieldlabel>ILIAS_VERSION</fieldlabel>

<fieldentry>5.3.8 2018-08-29</fieldentry>

</qtimetadatafield>


<qtimetadatafield>

<fieldlabel>QUESTIONTYPE</fieldlabel>

<fieldentry>CLOZE QUESTION</fieldentry>

</qtimetadatafield>


<qtimetadatafield>

<fieldlabel>AUTHOR</fieldlabel>

<fieldentry>Patrick Maik Mächler</fieldentry>

</qtimetadatafield>


<qtimetadatafield>

<fieldlabel>additional_cont_edit_mode</fieldlabel>

<fieldentry>default</fieldentry>

</qtimetadatafield>


<qtimetadatafield>

<fieldlabel>externalId</fieldlabel>

<fieldentry>5c3f83ce4b6880.97213190</fieldentry>

</qtimetadatafield>


<qtimetadatafield>

<fieldlabel>textgaprating</fieldlabel>

<fieldentry>ci</fieldentry>

</qtimetadatafield>


<qtimetadatafield>

<fieldlabel>fixedTextLength</fieldlabel>

<fieldentry>1</fieldentry>

</qtimetadatafield>


<qtimetadatafield>

<fieldlabel>identicalScoring</fieldlabel>

<fieldentry>1</fieldentry>

</qtimetadatafield>


<qtimetadatafield>

<fieldlabel>combinations</fieldlabel>

<fieldentry>W10=</fieldentry>

</qtimetadatafield>

</qtimetadata>

</itemmetadata>


<presentation label="titel">


<flow>


<material>

<mattext texttype="text/xhtml"><p>text1</p> <div id="OKAYFREEDOM_INJECTED" style="display: none;"></div></mattext>

</material>


<material>

<mattext texttype="text/xhtml"><p></p> <p>text2</p> <p></p> <p>auswahllücke</mattext>

</material>

第2部分:

<response_str ident="gap_0" rcardinality="Single">


<render_choice shuffle="No">


<response_label ident="0">


<material>

<mattext>auswahl1</mattext>

</material>

</response_label>


<response_label ident="1">


<material>

<mattext>auswahl2</mattext>

</material>

</response_label>

</render_choice>

</response_str>


<material>

<mattext texttype="text/plain">textlücke</mattext>

</material>


<response_str ident="gap_1" rcardinality="Single">

<render_fib maxchars="2" columns="8" prompt="Box" fibtype="String"/>

</response_str>


<material>

<mattext texttype="text/plain">numlücke</mattext>

</material>


<response_num ident="gap_2" rcardinality="Single" numtype="Decimal">

<render_fib maxchars="3" columns="2" prompt="Box" fibtype="Decimal" maxnumber="100" minnumber="1"/>

</response_num>


<material>

<mattext texttype="text/xhtml"></p> <p></p> <div></div> <p></p> <div></div> <div></div></mattext>

</material>

</flow>

</presentation>

第3部分:

<resprocessing>


<outcomes>

<decvar/>

</outcomes>


<respcondition continue="Yes">


<conditionvar>

<varequal respident="gap_0">auswahl1</varequal>

</conditionvar>

<setvar action="Add">1</setvar>

<displayfeedback linkrefid="0_Response_0" feedbacktype="Response"/>

</respcondition>


<respcondition continue="Yes">


<conditionvar>

<varequal respident="gap_0">auswahl2</varequal>

</conditionvar>

<setvar action="Add">2</setvar>

<displayfeedback linkrefid="0_Response_1" feedbacktype="Response"/>

</respcondition>


<respcondition continue="Yes">


<conditionvar>

<varequal respident="gap_1">antwort1</varequal>

</conditionvar>

<setvar action="Add">3</setvar>

<displayfeedback linkrefid="1_Response_0" feedbacktype="Response"/>

</respcondition>


<respcondition continue="Yes">


<conditionvar>

<varequal respident="gap_1">antwort2</varequal>

</conditionvar>

<setvar action="Add">4</setvar>

<displayfeedback linkrefid="1_Response_1" feedbacktype="Response"/>

</respcondition>


<respcondition continue="Yes">


<conditionvar>

<varequal respident="gap_2">99</varequal>

</conditionvar>

<setvar action="Add">5</setvar>

<displayfeedback linkrefid="2_Response_0" feedbacktype="Response"/>

</respcondition>

第4部分:

<itemfeedback ident="response_onenotcorrect" view="All">


<flow_mat>


<material>

<mattext texttype="text/xhtml"><p>respnotcorrect</p> <div id="OKAYFREEDOM_INJECTED" style="display: none;"></div></mattext>

</material>

</flow_mat>

</itemfeedback>

我不需要的部分,我只需要xml文件的这4部分。我该如何轻松拆分?有些元素被多次命名。

2 个答案:

答案 0 :(得分:0)

我将使用XMLDocument读取器/写入器。您可以在内存中操纵所需的元素。

可以在以下主题中找到大量文档:using xmldocument to read xml

答案 1 :(得分:0)

您仅在链接上发布了第1部分。下面我使用xml linq来获取元素。如果文件很大,则还需要使用xmlreader

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";

        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement resprocessing = doc.Descendants("resprocessing").FirstOrDefault();

            string header = "<?xml version=\"1.0\" encoding=\"utf-8\"?><resprocessing></resprocessing>";
            XDocument newDoc = XDocument.Parse(header);
            XElement newResprocessing = newDoc.Root;
            newResprocessing.ReplaceWith(resprocessing);
        }
    }
}