开发者

Adding Attribute to an XML node using an XMLSerializer

I'm new to this site so forgive my newbiness.

I'm working on a big system done in C# and I didn't found a way to do exactly what I want in the following situation.

I have an XML that contains something like this:

<XmlObject>
    <Info />
    <Objects>
        <Component>
            <FileData>C:\Folder\file.ext</FileData>
        <Component>
    </Objects>
</XmlObject>

And my goal is to get this:

<XmlObject>
    <Info />
    <Objects>
        <Component>
            <FileData Extension=".ext">C:\Folder\file.ext</FileData>
        <Component>
    </Objects>
</XmlObject>

But here are my constraints:

The XML file is created using an XmlSerializer and everything under the tag Objects is created by reflection so I can't add tags l开发者_Python百科ike [XmlElementAttribute] on FileData.

Currently we are using XmlAttributeOverrides to add the type returned by the reflection to the XmlSerializer. My problem is I don't know much about XmlAttributeOverrides and I can't find a way to add this NodeAttribute there.

Also, the value of Extension is the real extension of the element's value. Example:

<FileData Extension=".ext">C:\Folder\file.ext</FileData>
<FileData Extension=".png">C:\Folder\file.png</FileData>

A big thanks to everyone who will help find this or get me on a way to solve this.

EDIT: Last week, I found another to way to make this work. Instead of using XmlAttributeOverrides, I've created a new class that when serialized in an XML it could be readed as a simple string but with a XML tag that contains an attribute. When the application does the reflection, I detect the members that should be outputted like this and instead of emitting IL so it can be used as a string, I emitting IL so it can be used like my new class.

Still I won't close that question since I'm quite interested in knowing more about XmlAttributeOverrides and I'm quite sure some of you will teach me useful knowledge on that subject.

If some of you want example of what I've done for their own culture, just let me know, I'll try to create an example out of it since the code I'm working out is a property of the company I work for.


You can use this solution: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜