开发者

Use Dynamic or Reflection.emit

开发者 https://www.devze.com 2023-01-06 22:05 出处:网络
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:

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(percenta开发者_高级运维ge), 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消