0

I am stuck on a project which needs to exchange data between two programs who have different coontrol files. Program A just creates a a XML File whereas Program B needs the input as an array with two "columns" - but how do can I do the conversion?

Example: The xml looks like:

` <Presentation>
  <Options Name="1">
  <Output>MyOutPut</Output>
    <Slides Name="1">
    <Template>Template2</Template> 
      <Diagram Name="Name4">
         <More Elements...>
      </Diagram>
    </Slides>
    </Options>
  </Presentation>`

The output should like this:

 `
    Presentation ""
    Options 1
    Output Myoutput
    Slides 1
    Template Template2

...
`

But how can I achieve this? I am using vb.net and LINQ and have no idea how to solve this problem. I tried some LINQ queries but I get only indivudal nodes or attributes so I think it would be better to try a different idea. Any advice would be appreciated!

4

1 回答 1

0

不幸的是,我无法提供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);
}
于 2012-06-06T10:59:25.177 回答