Click here to Skip to main content
12,954,601 members (54,385 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi all,

I have an XML file that is created by a powershell script, each XML has differing amounts of nodes within it. An example of a shorter one is below:

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>...</T>
      <T>...</T>
      <T>...</T>
    </TN>
    <MS>
      <S N="LevelDisplayName">...</S>
      <S N="Message">...</S>
      <S N="MachineName">...</S>
      <I32 N="Id">...</I32>
    </MS>
  </Obj>
  <Obj RefId="1">
    <TNRef RefId="0" />
    <MS>
      <S N="LevelDisplayName">...</S>
      <S N="Message">...</S>
      <S N="MachineName">...</S>
      <I32 N="Id">...</I32>
    </MS>
  </Obj>
</Objs>


Now i want to get this XML file and convert it into a useable C# object. I've tried using a deserializer method but as the amount of nodes and what they are tagged as can change, i cannot create a class that accomodates these deserialized variables, so will error everytime.
public static List<T> RetrieveFromXML<T>(string filename, string root)
{
     // Declare an object variable of the type to be deserialized.
     List<T> t;
     using (FileStream fs = new FileStream(filename, FileMode.Open))
     {
          XmlSerializer ser = new XmlSerializer(typeof(List<T>), new  XmlRootAttribute(root));
          t = (List<T>)ser.Deserialize(fs);
      }
 
      return t;
}


Is there a way that an object can be dynamically created from the XML, or any other method that you guys have come across?
Posted 11-Nov-12 23:52pm
Comments
Storxstar 12-Nov-12 6:09am
   
have you tried using an XmlReader? If not try using one, with Multiple for loops too keep looping through nodes and returning values.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

You can use Dynamic of C# 4.0 feature. that may help you to generate object on the fly.

I found one link that may help you,

Converting XML to an dynamic object using ExpandoObject[^]

Best luck.
  Permalink  
Comments
MitchG92_24 14-Nov-12 5:22am
   
It hasn't worked for my use as it needs to operate on earlier .NET frameworks but it is a very useful feature so i shall be using in the future!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I have created an XDocument from the XML file itself and iterating through the nodes in the document and acting upon them differently using a switch statement. This has worked to how i want it.

//Counters
            int index = 0; //Increments through the array of Obj's
            int sIndex = 0; //Increments through the array of S's
 
            XDocument xmlDoc = XDocument.Load(file);
            List<obj> allObjects = new List<obj>();
            List<eventlog> eventLog = new List<eventlog>();
 
            foreach (var obj in xmlDoc.Descendants())
            {
                switch (obj.Name.LocalName.ToString())
                {
                    case "Obj":
                        {
                            Obj newObj = new Obj();
                            newObj.RefId = obj.Attribute("RefId").Value;
                            allObjects.Add(newObj);
                        }
                        break;
                    case "MS":
                        {
                            MS newMS = new MS();
                            allObjects[index].MyMS = newMS;
 
                        }
                        break;
                    case "S":
                        {
                            S newS = new S();
                            newS.Attribute = obj.Attribute("N").Value;
                            newS.Content = obj.Value.ToString();
                            allObjects[index].MyMS.S[sIndex] = newS;
                            sIndex++;
                            if (sIndex == 3)
                                sIndex = 0;
                        }
                        break;
                    case "I32":
                        {
                            I32 i32 = new I32();
                            i32.Id = Convert.ToInt32(obj.Value.ToString());
                            allObjects[index].MyMS.i32 = i32;
                            index++;
                        }
                        break;
                    default:
                        break;
                }
            }</eventlog></eventlog></obj></obj>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,539
CHill60 3,490
Maciej Los 3,153
ppolymorphe 2,030
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100