1

been going nuts over this, my xml:

<?xml version="1.0"?>
<ArrayOfWorItems xmlns="http://someUrl" xmlns:i="http://www.w3.org/">
<WorkItem>
    <ItemID>596</ItemID>
    <ItemPressure>35.0000</ItemPressure>
    <ItemTableID>14</ItemTableID>
    <ItemVersion>AAAAAAAADGQ=</ItemVersion>
    <ItemTemperature>-196.0000</ItemTemperature>
</WorkItem>
<WorkItem>
    <ItemID>596</ItemID>
    <ItemPressure>35.0000</ItemPressure>
    <ItemTableID>14</ItemTableID>
    <ItemVersion>AAAAAAAADGQ=</ItemVersion>
    <ItemTemperature>-196.0000</ItemTemperature>
</WorkItem>
<WorkItem>
    <ItemID>596</ItemID>
    <ItemPressure>35.0000</ItemPressure>
    <ItemTableID>14</ItemTableID>
    <ItemVersion>AAAAAAAADGQ=</ItemVersion>
    <ItemTemperature>-196.0000</ItemTemperature>
</WorkItem>
    <WorkItem>
    <ItemID>596</ItemID>
    <ItemPressure>35.0000</ItemPressure>
    <ItemTableID>14</ItemTableID>
    <ItemVersion>AAAAAAAADGQ=</ItemVersion>
    <ItemTemperature>-196.0000</ItemTemperature>
</WorkItem>

</ArrayOfWorItems>

I want to parse this to my class:

[XmlRoot("ArrayOfWorItems")]
public class ArrayOfWorItems
{
    public List<WorkItem> WorkItem { get; set; }
}

logic:

    var doc = new XmlDataDocument();

    doc.Load(path);


    XmlNamespaceManager xmlNS = new XmlNamespaceManager(doc.NameTable);

    xmlNS.AddNamespace("stupid_xmlns", @"http://someUrl");

    string contents = doc.OuterXml;

    MyXmlHandler handler = new MyXmlHandler();

    var t = handler.ParseXml<ArrayOfWorItems>(contents);

Parse method:

    public T ParseXml<T>(string  xmlStream)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        using (StringReader reader = new StringReader(xmlStream))
        {
            var items  = (T)(serializer.Deserialize(reader));

            return items;
        }
    }

Error:

InnerException = {"http://someUrl'> was not expected."} Message = "There is an error in XML document (1, 23)."

Been at this for a while and cant figure it out, how do I solve this?

0

2 Answers 2

1

The Root class needs to be specified with the Namespace attribute also .

[XmlRoot("ArrayOfWorItems", Namespace = "http://someUrl")]
Sign up to request clarification or add additional context in comments.

1 Comment

Accepting this answer since it was correct for the question, but in order to make all this work I have added another answer aswell. Thank you @Gowri Pranith
0

After consulting with a friend I managed to get all of it to work.

@Gowri Pranith Was actuually right on the money from start but I had put in the wrong namespace and once I figured that out I had yet another issue:

I am use to parsing json objects and hence the logic is different when parsing. Friend of mine informed me that I can not have a list property inside of my parent class since xml does not handle this the same way as when parsing json, the correct way for this to work would be :

[XmlRoot("ArrayOfWorItems", Namespace = "http://someUrl")]
public class ArrayOfWorItems: List<WorkItem>
{
}

The parent class inherits the list and the parser renders them as children

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.