1

i want to create a xml file with the data pushed by sql server and i want build the xml like this:

<Items Class="1" Grade="20" Score="150">
    <Item ID="1" TemplateID="11233" StrengthLevel="0" AttackCompose="0" DefendCompose="0" LuckCompose="0" AgilityCompose="0" IsBind="true" ValidDate="0" Count="1" />
    <Item ID="2" TemplateID="200216" StrengthLevel="0" AttackCompose="0" DefendCompose="0" LuckCompose="0" AgilityCompose="0" IsBind="true" ValidDate="0" Count="1" />
    <Item ID="3" TemplateID="11408" StrengthLevel="0" AttackCompose="0" DefendCompose="0" LuckCompose="0" AgilityCompose="0" IsBind="true" ValidDate="0" Count="2" />
  </Items>

And here's the most important part of my code to create it in c#:

Dictionary<int, Dictionary<int, DailyLeagueAwardList>> dictionary1 = new Dictionary<int, Dictionary<int, DailyLeagueAwardList>>();
      DailyLeagueAwardList[] allDailyLeagueAwardList = produceBussiness.GetAllDailyLeagueAwardList();
      DailyLeagueAwardItems[] allDailyLeagueAwardItems = produceBussiness.GetAllDailyLeagueAwardItems();
      foreach (DailyLeagueAwardList DailyLeagueAwardList in allDailyLeagueAwardList)
      {
        DailyLeagueAwardList.AwardLists = new List<DailyLeagueAwardItems>();
        if (!dictionary1.ContainsKey(DailyLeagueAwardList.Class))
          dictionary1.Add(DailyLeagueAwardList.Class, new Dictionary<int, DailyLeagueAwardList>()
          {
            {
              DailyLeagueAwardList.Grade,
              DailyLeagueAwardList
            }
          });
        else if (!dictionary1[DailyLeagueAwardList.Class].ContainsKey(DailyLeagueAwardList.Grade))
          dictionary1[DailyLeagueAwardList.Class].Add(DailyLeagueAwardList.Grade, DailyLeagueAwardList);
      }
      foreach (DailyLeagueAwardItems DailyLeagueAwardItems in allDailyLeagueAwardItems)
      {
        if (dictionary1.ContainsKey(DailyLeagueAwardItems.ID) && dictionary1[DailyLeagueAwardItems.ID].ContainsKey(DailyLeagueAwardItems.Class))
          dictionary1[DailyLeagueAwardItems.ID][DailyLeagueAwardItems.Class].AwardLists.Add(DailyLeagueAwardItems);
      }
      XElement xelement1 = (XElement) null;
      foreach (Dictionary<int, DailyLeagueAwardList> dictionary2 in dictionary1.Values)
      {
        foreach (DailyLeagueAwardList DailyLeagueAwardList in dictionary2.Values)
        {
          if (xelement1 == null)
            xelement1 = new XElement((XName) "Items", (object) new XAttribute((XName) "Class", 
                (object) DailyLeagueAwardList.Class), (object)new XAttribute((XName)"Grade", 
                (object)DailyLeagueAwardList.Grade), 
                (object)new XAttribute((XName)"Score", 
                (object)DailyLeagueAwardList.Score), 
                (object)new XAttribute((XName)"Rank", 
                (object)DailyLeagueAwardList.Rank));
          foreach (DailyLeagueAwardItems awardList in DailyLeagueAwardList.AwardLists)
          {
            XElement xelement3 = new XElement((XName) "Item", new object[10]
            {
              (object) new XAttribute((XName) "ID", (object) awardList.ID),
              (object) new XAttribute((XName) "TemplateID", (object) awardList.TemplateID),
              (object) new XAttribute((XName) "StrengthLevel", (object) awardList.StrengthLevel),
              (object) new XAttribute((XName) "AttackCompose", (object) awardList.AttackCompose),
              (object) new XAttribute((XName) "DefendCompose", (object) awardList.DefendCompose),
              (object) new XAttribute((XName) "LuckCompose", (object) awardList.LuckCompose),
              (object) new XAttribute((XName) "AgilityCompose", (object) awardList.AgilityCompose),
              (object) new XAttribute((XName) "IsBind", (object) awardList.IsBind),
              (object) new XAttribute((XName) "ValidDate", (object) awardList.ValidDate),
              (object) new XAttribute((XName) "Count", (object) awardList.Count)
            });
            xelement1.Add((object) xelement3);
          }
          //xelement1.Add((object) xelement3);
        }
        result.Add((object) xelement1);
        xelement1 = (XElement) null;
      }

I'm using XElement on C#, i think the problem is on ↓

foreach (DailyLeagueAwardItems DailyLeagueAwardItems in allDailyLeagueAwardItems)
      {
        if (dictionary1.ContainsKey(DailyLeagueAwardItems.ID) && dictionary1[DailyLeagueAwardItems.ID].ContainsKey(DailyLeagueAwardItems.Class))
          dictionary1[DailyLeagueAwardItems.ID][DailyLeagueAwardItems.Class].AwardLists.Add(DailyLeagueAwardItems);
      }

And above i think is the error, how i can fix it?

Thanks and sorry for the bad english!

*EDIT Is only returning this:

<Items Class="1" Grade="20" Score="150" Rank="0" />
  <Items Class="2" Grade="20" Score="200" Rank="0" />
  <Items Class="3" Grade="20" Score="250" Rank="0" />
  <Items Class="4" Grade="20" Score="300" Rank="0" />
  <Items Class="5" Grade="20" Score="400" Rank="0" />
  <Items Class="6" Grade="20" Score="550" Rank="0" />
  <Items Class="7" Grade="20" Score="700" Rank="0" />
  <Items Class="8" Grade="20" Score="850" Rank="0" />
  <Items Class="9" Grade="20" Score="900" Rank="0" />
  <Items Class="10" Grade="30" Score="150" Rank="0" />
  <Items Class="11" Grade="30" Score="200" Rank="0" />
  <Items Class="12" Grade="30" Score="250" Rank="0" />
  <Items Class="13" Grade="30" Score="300" Rank="0" />
  <Items Class="14" Grade="30" Score="400" Rank="0" />
  <Items Class="15" Grade="30" Score="550" Rank="0" />
  <Items Class="16" Grade="30" Score="700" Rank="0" />
  <Items Class="17" Grade="30" Score="850" Rank="0" />
  <Items Class="18" Grade="30" Score="900" Rank="0" />
  <Items Class="19" Grade="40" Score="150" Rank="0" />
  <Items Class="20" Grade="40" Score="200" Rank="0" />
  <Items Class="21" Grade="40" Score="250" Rank="0" />
  <Items Class="22" Grade="40" Score="300" Rank="0" />
  <Items Class="23" Grade="40" Score="400" Rank="0" />
  <Items Class="24" Grade="40" Score="550" Rank="0" />
  <Items Class="25" Grade="40" Score="700" Rank="0" />
  <Items Class="26" Grade="40" Score="850" Rank="0" />
  <Items Class="27" Grade="40" Score="900" Rank="0" />
  <Items Class="28" Grade="100" Score="0" Rank="1" />
  <Items Class="29" Grade="100" Score="0" Rank="2" />
  <Items Class="30" Grade="100" Score="0" Rank="3" />
Thiago
  • 23
  • 6
  • But what error do you have? – Jacky Nov 13 '17 at 02:01
  • Also, how is your sql server, what is the input? Is this `awardList` is the node `Items` ? – Jacky Nov 13 '17 at 02:07
  • I refresh the post with the result that has returning, i want and after – Thiago Nov 13 '17 at 02:47
  • It doesn't help us, either. Meaning no error, just `` can't be imported? – Jacky Nov 13 '17 at 02:53
  • Yes Jacky When I change the ID to Class or Class to ID in code above the 5 variables, I attempting to change multiple combinations worked once, however only the element – Thiago Nov 13 '17 at 03:08
  • This: foreach (DailyLeagueAwardItems DailyLeagueAwardItems in allDailyLeagueAwardItems) { if (dictionary1.ContainsKey(DailyLeagueAwardItems.ID) && dictionary1[DailyLeagueAwardItems.ID].ContainsKey(DailyLeagueAwardItems.Class)) dictionary1[DailyLeagueAwardItems.ID][DailyLeagueAwardItems.Class].AwardLists.Add(DailyLeagueAwardItems); } – Thiago Nov 13 '17 at 03:11

1 Answers1

1

After reading your question, your comment, I still don't understand, and I'm afraid, the more I ask, the more confusing I get.

If I was you, instead of doing this, I will go by 2 approach:

  1. If you have a string build in like this: (note: i simplify from your xml)

string str = "<Items Class=\"1\" Grade=\"20\" Score=\"150\"><Item ID=\"1\" TemplateID=\"11233\" /></Items>

Then what need to be done is :

XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
doc.Save("myFile.xml"); //Save XML to a physical file if needed
  1. Using Serialize:

From database, you should have following classes:

I believe your awardList belongs to Items class below

public class Items
{
    [XmlAttribute()]
    public int Class{get;set;}

    [XmlAttribute()]
    public int Grade{get;set;}

    [XmlAttribute()]
    public double Score{get;set;}


    [XmlElement("Item")]
    public List<Item> Item { get; set; }


}

public class Item
{
    [XmlAttribute()]
    public int ID{get;set;}

    [XmlAttribute()]
    public int TemplateID{get;set;}

    [XmlAttribute()]
    public int StrengthLevel{get;set;}

    [XmlAttribute()]
    public int AttackCompose{get;set;}

    [XmlAttribute()]
    public int DefendCompose{get;set;}

    [XmlAttribute()]
    public int LuckCompose{get;set;}

    [XmlAttribute()]
    public int AgilityCompose{get;set;}

    [XmlAttribute()]
    public bool IsBind{get;set;}

    [XmlAttribute()]
    public int ValidDate{get;set;}

    [XmlAttribute()]
    public int Count{get;set;}

}

Then at the end: (I copy from here, too lazy to re-type

XmlSerializer xsSubmit = new XmlSerializer(typeof(MyObject));
 var subReq = new MyObject();
 var xml = "";

 using(var sww = new StringWriter())
 {
     using(XmlWriter writer = XmlWriter.Create(sww))
     {
         xsSubmit.Serialize(writer, subReq);
         xml = sww.ToString(); // Your XML
     }
 }
Jacky
  • 2,924
  • 3
  • 22
  • 34
  • Is not like it that i want, i know, it’s to hard to explain and soo confuse, but thank you so much for help me anyway, for more people like you :)) – Thiago Nov 13 '17 at 05:00
  • @Thiago sometimes, rebuild everything will give you a fresh look. Anyway, using serialize can help you avoid human mistake compared to manual works – Jacky Nov 13 '17 at 05:18