Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

XML
<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.
C#
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
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.

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.
 
Share this answer
 
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!
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.

C#
//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>
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900