【发布时间】:2017-05-09 23:12:01
【问题描述】:
大家好,我正在尝试将下面的代码从 XDocument 转换为 XmlDocument(我需要在 XmlDocument 中使用它)您知道需要更改的不同方法吗?我在 XmlDocument 和 XElement 类中查找了其中一些,但我的更改没有编译。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement root = (XElement)doc.FirstNode;
DataTable dt = new DataTable();
dt.Columns.Add("Parent", typeof(string));
dt.Columns.Add("Child", typeof(string));
dt.Columns.Add("Attribute", typeof(string));
dt.Columns.Add("Value", typeof(string));
foreach(XElement parent in root.Elements())
{
string parentTag = parent.Name.LocalName;
foreach (XElement child in parent.Elements())
{
string childTag = child.Name.LocalName;
foreach (XAttribute attribute in child.Attributes())
{
dt.Rows.Add(new object[] { parentTag, childTag, attribute.Name, (string)attribute });
}
}
}
}
}
}
谢谢
【问题讨论】:
-
这些库彼此完全不同,但只要看看智能感知告诉你做什么,你就可以很快地重构这段代码。
-
为什么需要进行转换?
标签: c# linq-to-xml xmldocument