0

I'm trying to deserialize an item from XML value but I faced with an issue when some element has : value.

My XML is like this:

Actually, I don't have any namespace on that and it comes from an internal library that I don't have access to that.

<item>
    <dc:author>Value</dc:author>
</item>

My serialization part is:

XmlSerializer serializer = new XmlSerializer(typeof(Model));

using (StringReader reader = new StringReader(value))
{
     var data = (Model)serializer.Deserialize(reader);
}

And the model class is here:

[XmlRoot("item")]
public class Model
{
    [XmlElement(ElementName = "dc:author")]
    public string Author { get; set; }
}

I couldn't deserialize the data for the Author element.

What should I do when I have the : in xml value?

Saeid Mirzaei
  • 950
  • 2
  • 18
  • 48
  • Also check https://stackoverflow.com/questions/34331860/c-sharp-xml-deserialization-with-namespace – Progman Dec 29 '20 at 20:27
  • @Progman Thanks for sharing the links, I tried those but It doesn't work because I don't have namespace in my Author element. I tried like this [XmlElement(ElementName = "author", Namespace = "")] public string Author { get; set; } – Saeid Mirzaei Dec 29 '20 at 20:37
  • @Progman Actually, I don't have any other section in this XML, It just an item section. – Saeid Mirzaei Dec 29 '20 at 20:41
  • Where is the XML coming from and where is the "dc" coming from? That is part of an XML namespace prefix, but there is no namespace defined in your shown XML. Please [edit] your question to include the complete XML content you have and where it comes from. – Progman Dec 29 '20 at 20:43
  • @Progman Thanks, It comes from an Internal library that I don't have access to that and It just return an item without any namespace, right! I couldn't right the good question. – Saeid Mirzaei Dec 29 '20 at 20:50
  • @Progman Is it a good way that I clean this string by my-self? I mean with string.Replace or something else? – Saeid Mirzaei Dec 29 '20 at 20:53
  • You should fix the internal library to generate correct XML. But maybe you can check https://stackoverflow.com/questions/870293/can-i-make-xmlserializer-ignore-the-namespace-on-deserialization. – Progman Dec 29 '20 at 21:01
  • @Progman Thank you so much! It's so helpful because I don't have access to that library. – Saeid Mirzaei Dec 29 '20 at 21:03
  • You should speak with the person in charge of the internal library about this issue. – Progman Dec 29 '20 at 21:21

0 Answers0