Throwing exception on invalid xml element during xml serialization [duplicate]










0
















This question already has an answer here:



  • XML DeSerialize - Possible to Trap Extra XML in File?

    1 answer



  • XmlSerializer. Skip xml unknown node

    3 answers



  • xmlserializer validation

    2 answers



I am trying to do a mechanism in C# that deserializes an incoming Xml and validates whether it has invalid elements and thus, throws an exception, indicating the incorrect tag. Is it possible to achieve this with only xml serialization?



Example:



<myXml>
<tag1>ONE</tag1>
<tag2>TWO</tag2>
<tag3>THREE</tag3>
<tag4>FOUR</tag4>
</myXml>


And my C# definition for serialization should be something like this:



[XmlRoot(ElementName="myXml")]
public class MyXml
[XmlElement(ElementName="tag1")]
public string Tag1 get; set;
[XmlElement(ElementName="tag2")]
public string Tag2 get; set;



So, as you can see tag3 and tag4 will not be included within the deserialization process, but, what I also want to do is, since this xml does not match, then throw an exception with those extra fields not recognized.



Thanks for your help!










share|improve this question













marked as duplicate by dbc c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 22 '18 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    Create a schema and have tags as required.

    – jdweng
    Nov 13 '18 at 18:08











  • You can validate against an XSD but, in Microsoft's design, this is done at the XmlReader level not the XmlSerializer level, presumably so one could validate without deserializing. See xmlserializer validation for how.

    – dbc
    Nov 13 '18 at 21:01











  • But if you just want to trap unknown elements see XML DeSerialize - Possible to Trap Extra XML in File?, XmlSerializer. Skip xml unknown node, XmlSerializer: keep unknown elements or Deserializing XML with unknown element order. In fact this may be a duplicate, agree?

    – dbc
    Nov 13 '18 at 21:08















0
















This question already has an answer here:



  • XML DeSerialize - Possible to Trap Extra XML in File?

    1 answer



  • XmlSerializer. Skip xml unknown node

    3 answers



  • xmlserializer validation

    2 answers



I am trying to do a mechanism in C# that deserializes an incoming Xml and validates whether it has invalid elements and thus, throws an exception, indicating the incorrect tag. Is it possible to achieve this with only xml serialization?



Example:



<myXml>
<tag1>ONE</tag1>
<tag2>TWO</tag2>
<tag3>THREE</tag3>
<tag4>FOUR</tag4>
</myXml>


And my C# definition for serialization should be something like this:



[XmlRoot(ElementName="myXml")]
public class MyXml
[XmlElement(ElementName="tag1")]
public string Tag1 get; set;
[XmlElement(ElementName="tag2")]
public string Tag2 get; set;



So, as you can see tag3 and tag4 will not be included within the deserialization process, but, what I also want to do is, since this xml does not match, then throw an exception with those extra fields not recognized.



Thanks for your help!










share|improve this question













marked as duplicate by dbc c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 22 '18 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    Create a schema and have tags as required.

    – jdweng
    Nov 13 '18 at 18:08











  • You can validate against an XSD but, in Microsoft's design, this is done at the XmlReader level not the XmlSerializer level, presumably so one could validate without deserializing. See xmlserializer validation for how.

    – dbc
    Nov 13 '18 at 21:01











  • But if you just want to trap unknown elements see XML DeSerialize - Possible to Trap Extra XML in File?, XmlSerializer. Skip xml unknown node, XmlSerializer: keep unknown elements or Deserializing XML with unknown element order. In fact this may be a duplicate, agree?

    – dbc
    Nov 13 '18 at 21:08













0












0








0









This question already has an answer here:



  • XML DeSerialize - Possible to Trap Extra XML in File?

    1 answer



  • XmlSerializer. Skip xml unknown node

    3 answers



  • xmlserializer validation

    2 answers



I am trying to do a mechanism in C# that deserializes an incoming Xml and validates whether it has invalid elements and thus, throws an exception, indicating the incorrect tag. Is it possible to achieve this with only xml serialization?



Example:



<myXml>
<tag1>ONE</tag1>
<tag2>TWO</tag2>
<tag3>THREE</tag3>
<tag4>FOUR</tag4>
</myXml>


And my C# definition for serialization should be something like this:



[XmlRoot(ElementName="myXml")]
public class MyXml
[XmlElement(ElementName="tag1")]
public string Tag1 get; set;
[XmlElement(ElementName="tag2")]
public string Tag2 get; set;



So, as you can see tag3 and tag4 will not be included within the deserialization process, but, what I also want to do is, since this xml does not match, then throw an exception with those extra fields not recognized.



Thanks for your help!










share|improve this question















This question already has an answer here:



  • XML DeSerialize - Possible to Trap Extra XML in File?

    1 answer



  • XmlSerializer. Skip xml unknown node

    3 answers



  • xmlserializer validation

    2 answers



I am trying to do a mechanism in C# that deserializes an incoming Xml and validates whether it has invalid elements and thus, throws an exception, indicating the incorrect tag. Is it possible to achieve this with only xml serialization?



Example:



<myXml>
<tag1>ONE</tag1>
<tag2>TWO</tag2>
<tag3>THREE</tag3>
<tag4>FOUR</tag4>
</myXml>


And my C# definition for serialization should be something like this:



[XmlRoot(ElementName="myXml")]
public class MyXml
[XmlElement(ElementName="tag1")]
public string Tag1 get; set;
[XmlElement(ElementName="tag2")]
public string Tag2 get; set;



So, as you can see tag3 and tag4 will not be included within the deserialization process, but, what I also want to do is, since this xml does not match, then throw an exception with those extra fields not recognized.



Thanks for your help!





This question already has an answer here:



  • XML DeSerialize - Possible to Trap Extra XML in File?

    1 answer



  • XmlSerializer. Skip xml unknown node

    3 answers



  • xmlserializer validation

    2 answers







c# xml






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 16:58









ManuelManuel

132




132




marked as duplicate by dbc c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 22 '18 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by dbc c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 22 '18 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1





    Create a schema and have tags as required.

    – jdweng
    Nov 13 '18 at 18:08











  • You can validate against an XSD but, in Microsoft's design, this is done at the XmlReader level not the XmlSerializer level, presumably so one could validate without deserializing. See xmlserializer validation for how.

    – dbc
    Nov 13 '18 at 21:01











  • But if you just want to trap unknown elements see XML DeSerialize - Possible to Trap Extra XML in File?, XmlSerializer. Skip xml unknown node, XmlSerializer: keep unknown elements or Deserializing XML with unknown element order. In fact this may be a duplicate, agree?

    – dbc
    Nov 13 '18 at 21:08












  • 1





    Create a schema and have tags as required.

    – jdweng
    Nov 13 '18 at 18:08











  • You can validate against an XSD but, in Microsoft's design, this is done at the XmlReader level not the XmlSerializer level, presumably so one could validate without deserializing. See xmlserializer validation for how.

    – dbc
    Nov 13 '18 at 21:01











  • But if you just want to trap unknown elements see XML DeSerialize - Possible to Trap Extra XML in File?, XmlSerializer. Skip xml unknown node, XmlSerializer: keep unknown elements or Deserializing XML with unknown element order. In fact this may be a duplicate, agree?

    – dbc
    Nov 13 '18 at 21:08







1




1





Create a schema and have tags as required.

– jdweng
Nov 13 '18 at 18:08





Create a schema and have tags as required.

– jdweng
Nov 13 '18 at 18:08













You can validate against an XSD but, in Microsoft's design, this is done at the XmlReader level not the XmlSerializer level, presumably so one could validate without deserializing. See xmlserializer validation for how.

– dbc
Nov 13 '18 at 21:01





You can validate against an XSD but, in Microsoft's design, this is done at the XmlReader level not the XmlSerializer level, presumably so one could validate without deserializing. See xmlserializer validation for how.

– dbc
Nov 13 '18 at 21:01













But if you just want to trap unknown elements see XML DeSerialize - Possible to Trap Extra XML in File?, XmlSerializer. Skip xml unknown node, XmlSerializer: keep unknown elements or Deserializing XML with unknown element order. In fact this may be a duplicate, agree?

– dbc
Nov 13 '18 at 21:08





But if you just want to trap unknown elements see XML DeSerialize - Possible to Trap Extra XML in File?, XmlSerializer. Skip xml unknown node, XmlSerializer: keep unknown elements or Deserializing XML with unknown element order. In fact this may be a duplicate, agree?

– dbc
Nov 13 '18 at 21:08












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus