0

I have inner XML that looks like:

<approving xmlns="uz:rwc:fdu-92:1.0">
    <fdu92>
        <general num="25120001" date="25.12.2013" payment_kind="tehpd" sum="22200" />
        <station code="370006" name="Test" road_short_name="Test" />
        <cargo-owner code="7765" name="Test Subject" />
        <payer code="7395392" name="Test Subject" />
        <references>
            <reference num="11111" doc_name="soma document">
                <payments>
                    <payment code="158" subcode="001" name="Some work" reason="unknown" sum="22200" expended_count="1" />
                </payments>
            </reference>
        </references>
    </fdu92>
</approving>
<signature xmlns="uz:rwc:fdu-92:1.0">Many thousands of symbols</signature>

When I am trying to build classes from Xml by online tool xml to classes it return Invalid Xml.

Because it has

<signature xmlns="uz:rwc:fdu-92:1.0">Many thousands of symbols</signature>

Converting without tag

<signature xmlns="uz:rwc:fdu-92:1.0">Many thousands of symbols</signature> 

gives good result

namespace Xml2CSharp
{
    [XmlRoot(ElementName="general", Namespace="uz:rwc:fdu-92:1.0")]
    public class General {
        [XmlAttribute(AttributeName="num")]
        public string Num { get; set; }
        [XmlAttribute(AttributeName="date")]
        public string Date { get; set; }
        [XmlAttribute(AttributeName="payment_kind")]
        public string Payment_kind { get; set; }
        [XmlAttribute(AttributeName="sum")]
        public string Sum { get; set; }
    }

    [XmlRoot(ElementName="station", Namespace="uz:rwc:fdu-92:1.0")]
    public class Station {
        [XmlAttribute(AttributeName="code")]
        public string Code { get; set; }
        [XmlAttribute(AttributeName="name")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName="road_short_name")]
        public string Road_short_name { get; set; }
    }

    [XmlRoot(ElementName="cargo-owner", Namespace="uz:rwc:fdu-92:1.0")]
    public class Cargoowner {
        [XmlAttribute(AttributeName="code")]
        public string Code { get; set; }
        [XmlAttribute(AttributeName="name")]
        public string Name { get; set; }
    }

    [XmlRoot(ElementName="payer", Namespace="uz:rwc:fdu-92:1.0")]
    public class Payer {
        [XmlAttribute(AttributeName="code")]
        public string Code { get; set; }
        [XmlAttribute(AttributeName="name")]
        public string Name { get; set; }
    }

    [XmlRoot(ElementName="payment", Namespace="uz:rwc:fdu-92:1.0")]
    public class Payment {
        [XmlAttribute(AttributeName="code")]
        public string Code { get; set; }
        [XmlAttribute(AttributeName="subcode")]
        public string Subcode { get; set; }
        [XmlAttribute(AttributeName="name")]
        public string Name { get; set; }
        [XmlAttribute(AttributeName="reason")]
        public string Reason { get; set; }
        [XmlAttribute(AttributeName="sum")]
        public string Sum { get; set; }
        [XmlAttribute(AttributeName="expended_count")]
        public string Expended_count { get; set; }
    }

    [XmlRoot(ElementName="payments", Namespace="uz:rwc:fdu-92:1.0")]
    public class Payments {
        [XmlElement(ElementName="payment", Namespace="uz:rwc:fdu-92:1.0")]
        public Payment Payment { get; set; }
    }

    [XmlRoot(ElementName="reference", Namespace="uz:rwc:fdu-92:1.0")]
    public class Reference {
        [XmlElement(ElementName="payments", Namespace="uz:rwc:fdu-92:1.0")]
        public Payments Payments { get; set; }
        [XmlAttribute(AttributeName="num")]
        public string Num { get; set; }
        [XmlAttribute(AttributeName="doc_name")]
        public string Doc_name { get; set; }
    }

    [XmlRoot(ElementName="references", Namespace="uz:rwc:fdu-92:1.0")]
    public class References {
        [XmlElement(ElementName="reference", Namespace="uz:rwc:fdu-92:1.0")]
        public Reference Reference { get; set; }
    }

    [XmlRoot(ElementName="fdu92", Namespace="uz:rwc:fdu-92:1.0")]
    public class Fdu92 {
        [XmlElement(ElementName="general", Namespace="uz:rwc:fdu-92:1.0")]
        public General General { get; set; }
        [XmlElement(ElementName="station", Namespace="uz:rwc:fdu-92:1.0")]
        public Station Station { get; set; }
        [XmlElement(ElementName="cargo-owner", Namespace="uz:rwc:fdu-92:1.0")]
        public Cargoowner Cargoowner { get; set; }
        [XmlElement(ElementName="payer", Namespace="uz:rwc:fdu-92:1.0")]
        public Payer Payer { get; set; }
        [XmlElement(ElementName="references", Namespace="uz:rwc:fdu-92:1.0")]
        public References References { get; set; }
    }

    [XmlRoot(ElementName="approving", Namespace="uz:rwc:fdu-92:1.0")]
    public class Approving {
        [XmlElement(ElementName="fdu92", Namespace="uz:rwc:fdu-92:1.0")]
        public Fdu92 Fdu92 { get; set; }
        [XmlAttribute(AttributeName="xmlns")]
        public string Xmlns { get; set; }
    }

}

When I am trying to deserialize the object

DeserializeFromXmlElement<Approving>(FDU92Xml); 

public static T DeserializeFromXmlElement<T>(XmlElement element)
{
    var serializer = new XmlSerializer(typeof(T));
    return (T)serializer.Deserialize(new XmlNodeReader(element));
}

I get an error

xml document contains error

How to fix error? How to ignore signature in deserialization process?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
A191919
  • 3,422
  • 7
  • 49
  • 93
  • 1) What is the `FDU92Xml` element referring to at the time you call `DeserializeFromXmlElement(FDU92Xml);`. Is it the `` element or a containing element? 2) The reason that http://xmltocsharp.azurewebsites.net/ fails is likely that your XML is a **fragment** that does not have a single [root element](https://en.wikipedia.org/wiki/Root_element). – dbc Aug 17 '16 at 15:21
  • With that extra `Many thousands of symbols` at the end, this is no longer a **valid** XML document - it's an XML mess..... how to fix it? Make sure you have a **valid** XML ! Either by stripping out / omitting that extra `` at the end, or then by wrapping up that XML into an "artificial" ` .... ` element so that you get a valid XML – marc_s Aug 17 '16 at 15:22
  • @marc_s - OP is deserializing an inner node inside some larger XML document using an `XmlNodeReader` along the lines of [Deserialize object property with StringReader vs XmlNodeReader](https://stackoverflow.com/questions/30102275/deserialize-object-property-with-stringreader-vs-xmlnodereader). That's why the XML looks like a fragment. – dbc Aug 17 '16 at 15:25
  • Also, can you share the full `ToString()` output of the exception including the full message, exception type, traceback and inner exception (if any), and provide a [mcve] with showing a full XML document and how you are selecting the inner `XmlElement` to deserialize? – dbc Aug 17 '16 at 15:26
  • Have you tried using the `OuterXml` rather than the `InnerXml`? – Chris Dunaway Aug 17 '16 at 21:30

0 Answers0