Xml 转换数组中的整个Xdocument
我被困在一个项目中,需要在两个具有不同控制文件的程序之间交换数据。 程序A只是创建了一个XML文件,而程序B需要输入一个包含两列的数组——但是我如何进行转换呢 例如: xml看起来像:Xml 转换数组中的整个Xdocument,xml,vb.net,linq,Xml,Vb.net,Linq,我被困在一个项目中,需要在两个具有不同控制文件的程序之间交换数据。 程序A只是创建了一个XML文件,而程序B需要输入一个包含两列的数组——但是我如何进行转换呢 例如: xml看起来像: ` <Presentation> <Options Name="1"> <Output>MyOutPut</Output> <Slides Name="1"> <Template>Template2</Templ
` <Presentation>
<Options Name="1">
<Output>MyOutPut</Output>
<Slides Name="1">
<Template>Template2</Template>
<Diagram Name="Name4">
<More Elements...>
</Diagram>
</Slides>
</Options>
</Presentation>`
但我怎样才能做到这一点呢?我正在使用vb.net和LINQ,不知道如何解决这个问题。我尝试了一些LINQ查询,但我只得到独立的节点或属性,所以我认为最好尝试不同的想法。
任何建议都将不胜感激 不幸的是,我不能提供VB代码,但我希望这个C代码片段能有所帮助
void Main()
{
var doc = XElement.Load(@"path-to-the-file");
doc.DescendantsAndSelf()
.Select(ElementSelector).Dump();
}
private static KeyValuePair<string, string> ElementSelector(XElement e)
{
var name = e.Name.ToString();
var value = String.Empty;
if (e.Descendants().Any())
{
if (e.Attributes().Any())
{
value = e.Attributes().First().Value;
}
}
else
{
value = e.Value;
}
return new KeyValuePair<string, string>(name, value);
}
您好,雷皮尔,您的回答当场解决了我的问题。从c到vb.net的转换很容易,可以使用自动转换工具。但是,vb.net不知道dump,因此,我必须做一个小小的修改:''Private Sub-Main Dim doc=XElement.LoadMy.Application.Info.DirectoryPath&Constants.tempXML Dim Dump=doc.degenants和self.SelectAddressOf element选择器Dim list=Dump.ToList,用于列表Debug.Printelem.Key和elem.Value中的每个元素End Sub `你能提供一些ElementSelect做什么的信息吗?@ChristianSauer,我已经在LINQPad中测试了代码,Dump是一个内置方法,可以将结果写入控制台。ElementSelector是将XElement对象转换为键值对的方法;文档的每个元素都由ElementSelector方法投影到键值对中。
void Main()
{
var doc = XElement.Load(@"path-to-the-file");
doc.DescendantsAndSelf()
.Select(ElementSelector).Dump();
}
private static KeyValuePair<string, string> ElementSelector(XElement e)
{
var name = e.Name.ToString();
var value = String.Empty;
if (e.Descendants().Any())
{
if (e.Attributes().Any())
{
value = e.Attributes().First().Value;
}
}
else
{
value = e.Value;
}
return new KeyValuePair<string, string>(name, value);
}