我对LINQ很新,所以请耐心等待我:)
我目前正在尝试将XML格式的SSIS包(.dtsx)反向工程为.BIML文件,该文件也是基于XML的。但是它们对于相同的对象具有不同的构造。
所以我要做的是循环遍历.dtsx包的XML并基本检查元素的类型,并在新文件中创建一个等效元素,但具有不同的名称/属性,但是当我在新文件中创建元素时,我将需要保持对象的层次关系。
但我正在努力解决如何在循环浏览源文件的同时将新元素添加到新文件中。
有人能提供一些指示吗?
我现在能够遍历文件(我只是在某个时刻输出到控制台窗口,以检查是否我正在循环核心)但我正在努力将元素添加到新的文件
非常感谢任何帮助
string file = @"F:\\sample.dtsx"
XDocument xDoc = XDocument.Load(file);
XNamespace env = "www.microsoft.com/SqlServer/Dts";
IEnumerable<XElement> elements = xDoc.Root.Descendants(); //
XDocument BIMLXdoc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("Root"));
//BIMLXdoc.Add(new XElement("test")); ####This doesn't work
foreach (XElement element in elements)
{
// Test element and if of the correct type add new elemnt to biml file
IEnumerable<XAttribute> attribs = element.Attributes();
foreach (XAttribute attrib in attribs)
{
Console.WriteLine(element.Name.LocalName + " - Attribute(" + attrib.Name.LocalName + ") - Value:(" + attrib.Value + ")");
}
}
BIMLXdoc.Save("F:\\BIMLTest.xml");
答案 0 :(得分:0)
在注释行中,您尝试将节点添加到顶层。正确的XML文档有only one root element。因此,将节点添加到Root
元素:
var BIMLXdoc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("Root"));
BIMLXdoc.Root.Add(new XElement("test")); // This works
答案 1 :(得分:0)
如果要向Xdoc添加新元素,则需要将其添加到特定位置:
变化:
BIMLXdoc.Add(new XElement("test"));
致:
BIMLXdoc.Element("Root").Add(new XElement("TestChild", "TestChildValue"));
你将拥有一个子元素。