Git Product home page Git Product logo

Comments (4)

Cinchoo avatar Cinchoo commented on August 25, 2024

You POCO object structure must be defined as below to deserialize your xml successfully.

public class EmployeeRec1
{
    [ChoXmlNodeRecordField(XPath = "@Id")]
    public int Id { get; set; }
    [ChoXmlNodeRecordField(XPath = "Name")]
    public string Name { get; set; }
    [ChoXmlNodeRecordField(XPath = "/Address/*")]
    public AddressLineRec[] Address { get; set; }
}
public partial class AddressLineRec
{
    [XmlAttribute(AttributeName = "Id")]
    public int Id { get; set; }
    [XmlText]
    public string AddressLine { get; set; }
}

Hope it helps.

from choetl.

code2learnit avatar code2learnit commented on August 25, 2024

Thanks!! It works :)

I see you completly by-pass the AddressRec class, since it contains only an array of AddressLine.
But what happens if I have a more "complex" situation where there are AddressRec contains more fields (possibly other arrays) or even AddressLineRec contains other fields or array of fields

Is there any way or reason to include back AddressRec?

I tried to modify your sample to manage the following xml

<Employees>
    <Employee Id='1'>
        <Name>Tom</Name>
        <Address>
            <AddressLine Id='1'>XYZ road</AddressLine>
            <AddressLine Id='2'>MiceTown</AddressLine>
            <Country>United States</Country>
        </Address>
    </Employee>
    <Employee Id='2'>
        <Name>Mark</Name>
        <Address>
            <AddressLine Id='1'>123 street</AddressLine>
            <AddressLine Id='2'>TigerCity</AddressLine>
            <Country>Italy</Country>
        </Address>
    </Employee>
</Employees>

I understand that I can flat in some way the struct introducing a Country field in EmployeeRec

    public class EmployeeRec
    {
        [ChoXmlNodeRecordField(XPath = "@Id")]
        public int Id { get; set; }
        [ChoXmlNodeRecordField(XPath = "Name")]
        public string Name { get; set; }
        [ChoXmlNodeRecordField(XPath = "Address/AddressLine")] 
        public AddressLineRec[] Address { get; set; }
        [ChoXmlNodeRecordField(XPath = "Address/Country")]
        public string Country { get; set; }
    }

But was not able to put Country and AddressLines in a distinct class :(

sorry for "abusing" of your knowledge and patience

TIA
stefano

from choetl.

Cinchoo avatar Cinchoo commented on August 25, 2024

In that case, you can use the original object structure with few tweaks as below

public class EmployeeRec1
{
    [ChoXmlNodeRecordField(XPath = "@Id")]
    public int Id { get; set; }
    [ChoXmlNodeRecordField(XPath = "Name")]
    public string Name { get; set; }
    [ChoXmlNodeRecordField(XPath = "/Address")]
    public AddressRec Address { get; set; }
}

public partial class AddressRec
{
    [XmlElement(ElementName = "AddressLine")]
    public AddressLineRec[] AddressLines { get; set; }
    [XmlElement(ElementName = "Country")]
    public string Country { get; set; }
}

public partial class AddressLineRec
{
    [XmlAttribute(AttributeName = "Id")]
    public int Id { get; set; }
    [XmlText]
    public string AddressLine { get; set; }
}

from choetl.

code2learnit avatar code2learnit commented on August 25, 2024

Great! Thank you very much!!

I was trying to use ChoXmlNodeRecordField in the AddressRec class, and it did not work :(

from choetl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.