4
[XmlRootAttribute("Order",IsNullable = false)]    
public class Order
{
    [XmlAttribute("pay_method")]
    public string paymethod { get; set; }

    [XmlAttribute("name")]
    public string custid { get; set; }

    [XmlArray("")]
    [XmlArrayItem("Cargo", IsNullable = false)]
    public Cargo[] Cargos { get; set; }

    [XmlArray("")]
    [XmlArrayItem("AddedService", IsNullable = false)]
    public AddedService[] AddedServices { get; set; }
}

XmlSerializer xs = new XmlSerializer();
var requeststring = xs.Serialize(request);

When I run this function, in the result I always get <Cargos> like this:

<Order pay_method="" custid="">
    <Cargos>
    <Cargo name=""/>
    <Cargo name=""/>
    </Cargos>
    <AddedServices>
        <AddedService name=""/>
        <AddedService name=""/>
    </AddedServices>
</Order>

I just want to get the result without <Cargos></Cargos></AddedServices><AddedServices>, and for example:

<Order pay_method="" custid="">
    <Cargo name=""/>
    <Cargo name=""/>
    <AddedService name=""/>
    <AddedService name=""/>
</Order>

How can I config with both:

public Cargo[] Cargos { get; set; } 

public AddedService[] AddedServices { get; set; }
2
  • What you want would be considered invalid XML, so i don't really see a way or reason to achieve this. The problem is that A doesn't contain the Cargo items but a Collection of them, named Cargos - the current result is the exact representation of your object Commented Sep 21, 2017 at 8:44
  • You could, however, change your structure so A is a collection, like this: public class A: List<Cargo> - however, this would be a List instead of an array Commented Sep 21, 2017 at 8:45

1 Answer 1

2

You should not work with [XmlArrayItem] then, but work with [XmlElement(ElementName = "...")]

This should match your expected XML:

[XmlRoot(ElementName = "Cargo")]
public class Cargo
{
    [XmlAttribute(AttributeName = "name")]
    public string Name { get; set; }
}

[XmlRoot(ElementName = "AddedService")]
public class AddedService
{
    [XmlAttribute(AttributeName = "name")]
    public string Name { get; set; }
}

[XmlRoot(ElementName = "Order")]
public class Order
{
    [XmlElement(ElementName = "Cargo")]
    public List<Cargo> Cargo { get; set; }
    [XmlElement(ElementName = "AddedService")]
    public List<AddedService> AddedService { get; set; }
    [XmlAttribute(AttributeName = "pay_method")]
    public string Pay_method { get; set; }
    [XmlAttribute(AttributeName = "custid")]
    public string Custid { get; set; }
}

edit: Just tried it (and it works):

class Program
{
    static void Main(string[] args)
    {
        XmlSerializer xsSubmit = new XmlSerializer(typeof(Order));
        var subReq = new Order { Cargo  = new List<Cargo>{new Cargo { Name = "test" }, new Cargo { Name = "foo" }}, AddedService = new List<AddedService>{new AddedService{Name="addedService"}}, Custid = "custId", Pay_method = "bla"};
        var xml = "";

        using (var sww = new StringWriter())
        {
            using (XmlWriter writer = XmlWriter.Create(sww))
            {
                xsSubmit.Serialize(writer, subReq);
                xml = sww.ToString(); // Your XML
            }
        }
    }
}

Output (xml-variable):

<Order pay_method="bla" custid="custId">
    <Cargo name="test" />
    <Cargo name="foo" />
    <AddedService name="addedService" />
</Order>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks you answer,but it cant solve my problem.I have edit my question again, and please help me again.
Thank you very much. I know why i cant get. Beacause i using a not right namespace
OK,isee and i will do as what you say.

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.