문제

I get an HybridDictionary with all the child XElements.
I don't know ahead how many items I have there.
So instead of doing this:

xmlDoc.Element("Parent").Add(
     new XElement("Child", new XAttribute("Name", "Child1"),
           new XElement("Id", "796"),
           new XElement("Name", "gdsa")
           etc..
      ));  

I'm trying to do something like that:

Strung [] allKeys = new String[ChildElements.Count];
TheHybridDictionary.Keys.CopyTo(allKeys, 0);

xmlDoc.Element("Parent").Add(
      new XElement("Child", new XAttribute("Name", "Child1"),
            for (int i = 0; i < TheHybridDictionary.Count; i++)
                   new XElement(allKeys[i], TheHybridDictionary[allKeys[i]])

But how to connect whatever is inside the for loop to be part of the XML document construction?

도움이 되었습니까?

해결책

Problem is, your HybridDictionary class does not implement IEnumerable, so you can't use LINQ on it directly.

But you can use allKeys string array instead:

string [] allKeys = new String[ChildElements.Count];
TheHybridDictionary.Keys.CopyTo(allKeys, 0);

xmlDoc.Element("Parent").Add(
    new XElement("Child", new XAttribute("Name", "Child1"),
        allKeys.Select(x => new XElement(x, TheHybridDictionary[x])))

다른 팁

You can use .Add() method as in Stefan's comment, or use LINQ:

xmlDoc.Element("Parent").Add(
  new XElement("Child", new XAttribute("Name", "Child1"),
        TheHybridDictionary.Select(kvp => new XElement(kvp.Key, kvp.Value)));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top