views:

55

answers:

1

I'm new to C# and would like to ask for some direction on solving the following problem.

I've got a xml file used as a template (without knowing its content in advance). something like:

<Object>
  <Property name="ID">
    <Value weight="40">10000</Value>
    <Value weight="60">20000</Value>
  </Property>
  <Property name="Name">
    <Value weight="">foo</Value>
    <Value weight="">bar</Value>
  </Property>
  <Property name="Department">
    <SubProperty name="Department_ID">
      <Value weight="20">D01</Value>
      <Value weight="80">D02</Value>
    </SubProperty>
    <SubProperty name="Location">
      <Value weight="30">F01</Value>
      <Value weight="70">F02</Value>
    </SubProperty>
  </Property>
</Object>

I would like to read it in, do some shuffle, and export to a new xml file. Say, get the value of each property randomly, in accordance to their weights(percentage), to create a new list of mix-propertied objects, then serialize it to a new xml file.

Will this be done using Reflection.Emit to create a new "class" at runtime? Or is there any new features, like DynamicObject of C# 4.0 that I can use?

Any comment/sample is appreciated, thanks!

+1  A: 

It seems to me that XDocument would meet your needs well. With all of the LINQ operators you can use to query and transform the document, it should be easy and even enjoyable.

codekaizen
Thanks for your comment.I did try LINQ, though it worked well, but the code will increase alone with the growing of the properties in the template.So I just wondered if there's is some dynamic/robust way to solve this, even if I changed the whole property names of the template.thanks
Gnavvy
My thinking would be to use a query-first approach on the data to see what you have, and then perhaps build a lookup table of property names which you can use to access specific nodes...
codekaizen