.net 将ONIX XML作为数据集导入忽略HTML标记

.net 将ONIX XML作为数据集导入忽略HTML标记,.net,xml,.net,Xml,不久前,我编写了一个将ONIX文件导入零售数据库系统的过程。(ONIX是出版商用于发布其目录信息的XML标准。)该过程将XML文件直接导入到数据集中,对我们收到的大多数文件都很有效,但偶尔会出现例外情况。在这种特殊情况下,我尝试导入的文件在产品描述字段中包含HTML标记,这会破坏标准的Dataset.ReadXML()方法,因为它试图将HTML标记解释为XML。某些ONIX文件包含CDATA标记,可避免此问题,但在这种情况下,发布者选择使用标记属性指定字段为HTML格式,如下所示: &l

不久前,我编写了一个将ONIX文件导入零售数据库系统的过程。(ONIX是出版商用于发布其目录信息的XML标准。)该过程将XML文件直接导入到数据集中,对我们收到的大多数文件都很有效,但偶尔会出现例外情况。

在这种特殊情况下,我尝试导入的文件在产品描述字段中包含HTML标记,这会破坏标准的Dataset.ReadXML()方法,因为它试图将HTML标记解释为XML。某些ONIX文件包含CDATA标记,可避免此问题,但在这种情况下,发布者选择使用标记属性指定字段为HTML格式,如下所示:

    <othertext>
        <d102>03</d102>
        <d104 textformat="05">
            <p>Enter a world where bloody battles, and heroic deeds combine in the historic struggle to unite Britain in the face of a common enemy.</p>
            <p>The third instalment in Bernard Cornwell’s King Alfred series, follows on from the outstanding previous novels The Last Kingdom and The Pale Horseman.</p>
            <p>The year is 878 and the Vikings have been thrown out of Wessex. Uhtred, fresh from fighting for Alfred in the battle to free Wessex, travels north to seek revenge for his father's death, killed in a bloody raid by Uhtred's old enemy, renegade Danish lord, Kjartan.</p>
            <p>While Kjartan lurks in his formidable stronghold of Dunholm, the north is overrun by chaos, rebellion and fear. Together with a small band of warriors, Uhtred plans his attack on his enemy, revenge fuelling his anger, resolute on bloody retribution. But, he finds himself betrayed and ends up on a desperate slave voyage to Iceland. Rescued by a remarkable alliance of old friends and enemies, he and his allies, together with Alfred the Great, are free to fight once more in a battle for power, glory and honour.</p>
            <p>‘The Lords of the North’ is a tale of England's making, a powerful story of betrayal, struggle and romance, set in an England torn apart by turmoil and upheaval.</p>
        </d104>
    </othertext>

03
进入一个血战和英雄事迹相结合的世界,在面对共同敌人的历史斗争中团结英国

伯纳德·康威尔的《阿尔弗雷德国王》系列中的第三部是继之前杰出的小说《最后的王国》和《苍白骑士》之后的

公元878年,维京人被赶出了威塞克斯。Uhtred刚从解放韦塞克斯的战斗中为阿尔弗雷德而战,他向北旅行,为他父亲的死亡寻求复仇,父亲在Uhtred的宿敌、叛逆的丹麦领主Kjartan的血腥袭击中丧生

当克雅尔坦潜伏在他可怕的据点敦煌时,北方却被混乱、叛乱和恐惧所笼罩。Uhtred和一小队战士一起计划攻击他的敌人,复仇激起了他的愤怒,对血腥的报复表示坚决。但是,他发现自己被出卖了,最终绝望地踏上了前往冰岛的奴隶之旅。他和他的盟友,以及阿尔弗雷德大帝,在一个由老朋友和敌人组成的非凡联盟的拯救下,可以自由地再次为权力、荣誉和荣誉而战

《北方领主》是一部讲述英国历史的故事,讲述了背叛、斗争和浪漫的故事,故事背景是一个被动荡和动荡撕裂的英国

textformat=“05”属性表示HTML

在不编写用于解释HTML的自定义代码的情况下,是否仍然可以使用ReadXML()导入此内容,或者是否需要先以编程方式插入CDATA标记才能解决此问题?

注意:我不想去掉HTML标记,因为数据将显示在网站上。

这里有一个程序,它应该找到textformat=05节点,并将其内容包装在CData部分。看到这个了吗

void Main()
{
字符串xml=@“
03
进入一个血战和英雄事迹相结合的世界,在面对共同敌人的历史斗争中团结英国

伯纳德·康威尔的《阿尔弗雷德国王》系列中的第三部是继之前杰出的小说《最后的王国》和《苍白骑士》之后的

公元878年,维京人被赶出了威塞克斯。在解放威塞克斯的战斗中,刚为阿尔弗雷德战斗过的厄特雷德向北旅行,为他父亲的死亡寻求复仇。厄特雷德的宿敌、叛逆的丹麦领主克雅坦在一次血腥袭击中丧生

当克雅尔坦潜伏在他强大的据点邓霍姆时,北方被混乱、叛乱和恐惧所笼罩。乌特瑞德和一小队战士一起,计划攻击他的敌人,复仇激起了他的愤怒,决心进行血腥的报复。但是,他发现自己被出卖了,最终绝望地前往冰岛。他被一名海军营救他和他的盟友们,以及阿尔弗雷德大帝,组成了一个由老朋友和敌人组成的非凡联盟,他们可以自由地再次为权力、荣誉和荣誉而战

《北方领主》是一部讲述英国历史的故事,讲述了背叛、斗争和浪漫的故事,故事背景是一个被动荡和动荡撕裂的英国

"; XmlDocument xmlDoc=新的XmlDocument(); LoadXml(xml); var nodes=xmlDoc.SelectNodes(“//othertext/*[@textformat='05']”); foreach(节点中的XmlNode节点) { var cdata=xmlDoc.createCDATA节(node.InnerXml); node.InnerText=string.Empty; node.AppendChild(cdata); node.InnerXml.Dump(); } }
谢谢-成功了!我唯一需要做的调整是在下面一行(注意双斜杠):
var nodes=xmlDoc.SelectNodes(“//othertext/*[@textformat='05']”)
void Main()
{
    string xml = @"<othertext>
            <d102>03</d102>
            <d104 textformat=""05"">
                <p>Enter a world where bloody battles, and heroic deeds combine in the historic struggle to unite Britain in the face of a common enemy.</p>
                <p>The third instalment in Bernard Cornwell’s King Alfred series, follows on from the outstanding previous novels The Last Kingdom and The Pale Horseman.</p>
                <p>The year is 878 and the Vikings have been thrown out of Wessex. Uhtred, fresh from fighting for Alfred in the battle to free Wessex, travels north to seek revenge for his father's death, killed in a bloody raid by Uhtred's old enemy, renegade Danish lord, Kjartan.</p>
                <p>While Kjartan lurks in his formidable stronghold of Dunholm, the north is overrun by chaos, rebellion and fear. Together with a small band of warriors, Uhtred plans his attack on his enemy, revenge fuelling his anger, resolute on bloody retribution. But, he finds himself betrayed and ends up on a desperate slave voyage to Iceland. Rescued by a remarkable alliance of old friends and enemies, he and his allies, together with Alfred the Great, are free to fight once more in a battle for power, glory and honour.</p>
                <p>‘The Lords of the North’ is a tale of England's making, a powerful story of betrayal, struggle and romance, set in an England torn apart by turmoil and upheaval.</p>
            </d104>
        </othertext>";

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);
    var nodes = xmlDoc.SelectNodes("//othertext/*[@textformat='05']");
    foreach(XmlNode node in nodes)
    {
        var cdata = xmlDoc.CreateCDataSection(node.InnerXml);
        node.InnerText = string.Empty;
        node.AppendChild(cdata);
        node.InnerXml.Dump(); 
    }
}