Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am serializing a class in C# following this example

http://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.110).aspx

Here is my test class


[Serializable]
public class Test
{
   [XmlElement("SomeXmlElementName")]
   public int Something { get; set; }
}



This works well and creates and when the class content is serialized it creates a file with a node named SomeXmlElementName with the right value.

What I would also like is to include a comment in the XML file and I am looking for some .Net attribute I can use to decorate the method to include the comment. The code below does not compile, but it demonstrates the sort of thing I am after

[Serializable]
public class Test
{
   [XmlElement("SomeXmlElementName"), XmlComment("Some comment to include in the XML file when created"]
   public int Something { get; set; }
}


Does something like this exist at all?

Thank you
Posted
Updated 21-Oct-17 14:34pm
Comments
Sergey Alexandrovich Kryukov 4-Feb-14 14:43pm    
I wonder why? Normally, serialization XML should not use any comments. Why?
—SA
Jon Joeney 5-Feb-14 13:03pm    
Well I am serialising the XML for use by another application that defines the structure using an XML Schema that I cannot change. I want to add the time generated and other stuff into the XML in comments and so that is still schema valid but have a handle on when the file was created.
Sergey Alexandrovich Kryukov 5-Feb-14 13:14pm    
What a second... are you saying there is a schema which defines some comments? Maybe it even supposed to carry some information in comments? "Extending" the schema you cannot extend?
Anyway, I would not do such things and would advise you to rethink it. Way too dirty approach, unsafe, etc...
—SA
rhyous 16-Jul-15 13:18pm    
Xml can be displayed on web pages with an XSLT very similar to HTML. Comments might need to be displayed.

Also, an XML might need to be output with a EULA in comments.
Sergey Alexandrovich Kryukov 16-Jul-15 13:27pm    
The only right approach is to ignore comments. They should only be used for their intended purpose. Of course, an XML parser can read comments, but this is not for anything like displaying them or interpreting in any way. Why not following the design of XML?
—SA

1 solution

See my comment on your question.
If you want to write arbitrary XML code, you might have a look at XMLWriter Tutorial[^] and Writing XML with the XmlWriter[^] as well as XML Document Object Model (DOM)[^].
Cheers
Andi
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Feb-14 13:22pm    
I don't see how can it solve OP's problem, where the intervention is XML serialization is needed. Please see our comments to the question. My response was to give up this as a bad idea; and I cannot suggest anything in replacement; I could probably do it if I knew more about the big picture (writing some other files/streams in parallel, whatever). In the past, I already faced with the vague "requirements" to support some informational extensions to data in comment and denied to do it. There are such things, not doing them is more important then any features...
—SA
Andreas Gieriet 5-Feb-14 14:59pm    
I see your point. But I'm not that strict as you are. The OP found another solution that solves his problem, even I consider it a hack if he has the whole chain under his control.
Cheers
Andi
Sergey Alexandrovich Kryukov 5-Feb-14 15:19pm    
This is not just about strictness. My first point was: I don't see how your solution could help. OP is not using a stand-along XML generation, but serialization where you don't directly control the XML output. Can you see the point?
—SA
Andreas Gieriet 6-Feb-14 4:03am    
See my sentence: "If you want to write **arbitrary** XML code". It is not clear to me if the OP's use of the built-in serializing support is what is adequate for his problem. That's why I write "if you want to write **arbitrary** XML code...". We seem to read and interprete the question in a different way... ;-)
Cheers
Andi
Sergey Alexandrovich Kryukov 6-Feb-14 10:59am    
I think OP needs built-in serializing support; and this is the only real problem. If it was just writing some comments somewhere, it would not be a problem at all.
—SA

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