问题描述
我有一个政府文件的 XML 文件和它引用的 XSD 文件.我正在尝试从 XML(化学名称、UN 和危险分类)中提取信息并将其存储到 SQL Server 2014 中的表中.但是,每当我使用 SSIS 对 XML 源执行 ETL 时,我都会收到错误消息: XML 源适配器不支持复杂类型的混合内容模型
.我该如何解决这个问题?我想过并试图更好地理解的一些想法是:在 XML 上使用 XSLT 转换仅保留相关信息或使用 C# 和 XPath 仅选择我想要的节点.但这变得比我原先想象的要困难一些.任何帮助或方向表示赞赏.
I have an XML file for a government document and a XSD file that it references. I'm trying to extract information from the XML (chemical name, UN, and hazard classification) and store it into a table in SQL Server 2014. However, whenever I perform an ETL with an XML source, using SSIS, I get the error: The XML Source Adapter does not support mixed content model on Complex Types
. How can I work around this? Some ideas I thought about and tried to understand better were: using an XSLT transform on the XML to only retain the pertinent information or use C# and XPath to select only the nodes I want. But this has turned into something a bit harder than I originally thought. Any help or direction is appreciated.
http://www.gpo.gov/fdsys/bulkdata/CFR/2014/title-49/CFR-2014-title49-vol2.xmlhttp://www.gpo.gov/fdsys/bulkdata/CFR/resources/CFRMergedXML.xsd
推荐答案
试试这个
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\CFR-2014-title49-vol2.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement chapter = doc.Descendants("GPOTABLE").Where(x => x.Element("TTITLE") != null && x.Element("TTITLE").Value == "§ 172.101 Hazardous Materials Table").FirstOrDefault();
var results = chapter.Elements().Where(x => x.Name == "ROW" && x.Attribute("RUL") == null && (x.Elements("ENT").Count() == 14 || x.Elements("ENT").Count() == 2)).Select(y => new
{
chemical_name = y.Elements("ENT").Count() == 14 ? y.Elements("ENT").Skip(1).FirstOrDefault().Value
: y.Elements("ENT").Skip(1).FirstOrDefault().Value == ""
? string.Join("", y.Elements("ENT").Skip(1).FirstOrDefault().Descendants().Select(z => z.NextNode == null ? z.Value : z.Value + z.NextNode.ToString()).ToArray())
: string.Join("", y.Elements("ENT").Skip(1).FirstOrDefault().Value),
classification = y.Elements("ENT").Count() == 14 ? y.Elements("ENT").Skip(2).FirstOrDefault().Value : null,
UN = y.Elements("ENT").Count() == 14 ? y.Elements("ENT").Skip(3).FirstOrDefault().Value : null
}).Where(a => a.chemical_name.Length > 0).ToList();
}
}
}
这篇关于具有复杂类型 SSIS 错误的 XML 混合内容模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!