简体   繁体   中英

C# XML serialization query attribute without name

I need to create XML like below after using XMLSerializer. I created class as shown below.

Required Output:

<Item Price="000000002659"UnitOfMeasure="GALLONS">
    <TaxAmount Included="TRUE" Amount="000000000174"/>

Class definition :

public class Item
{           
    [XmlAttribute()]
    public string Price;
    [XmlAttribute()]
    public string UnitOfMeasure;      
    [XmlElement("")]
    public TaxDetails taxdetails;
}

public class TaxDetails
{
    [XmlAttribute()]
    public string TaxAmountIncluded;
    [XmlAttribute()]
    public string Amount;
}

After i create Item object with values, i would use ObjectToXMLString() to create XML. however this class definition creates the output like

<Item Price="000000002219" UnitOfMeasure="GALLONS">
    <taxdetails TaxAmountIncluded="Y" Amount="000000000000" />

I need to get rid of tag name taxdetails, but not sure how to define the TaxDetails class that would do that. Can anyone help me with definitions.

The field in the Item class is named taxdetails , which is why that's the default element name as well.

You can override the element name and attribute name by specifying the name you want in the attributes from System.Xml.Serialization , or you can rename your class members.

I've done one of each in this example - renamed the member of Item , and specified the name of the attribute under TaxDetails .

public class Item
{
    [XmlAttribute()]
    public string Price;
    [XmlAttribute()]
    public string UnitOfMeasure;
    [XmlElement()]
    public TaxDetails TaxAmount;
}

public class TaxDetails
{   
    [XmlAttribute("Included")]
    public string TaxAmountIncluded;
    [XmlAttribute()]
    public string Amount;
}

If you want to have the XML look like this:

<Item Price="000000002659"UnitOfMeasure="GALLONS">
    <TaxAmount Included="TRUE" Amount="000000000174"/>

And your current attempt looks like this:

<Item Price="000000002219" UnitOfMeasure="GALLONS">
    <taxdetails TaxAmountIncluded="Y" Amount="000000000000" />

You can see the two differences. "TaxAmount" is called "taxdetails", and "Amount" is called "TaxAmountIncluded". What you need to do is change the name of your properties so that they match:

public class Item
{           
    [XmlAttribute()]
    public string Price;
    [XmlAttribute()]
    public string UnitOfMeasure;      
    [XmlElement("")]
    public TaxDetails TaxAmount;
}

public class TaxDetails
{
    [XmlAttribute()]
    public string Included;
    [XmlAttribute()]
    public string Amount;
}

Basically rename TaxAmountIncluded to Included , then rename taxdetails to TaxAmount

Try This:

public class Item
{           
    [XmlAttribute()]
    public string Price;
    [XmlAttribute()]
    public string UnitOfMeasure;      
    [XmlElement("")]
    public TaxDetails TaxAmount;
}

public class TaxDetails
{
    [XmlAttribute()]
    public string Included;
    [XmlAttribute()]
    public string Amount;
}

or

public class Item
    {           
        [XmlAttribute()]
        public string Price;
        [XmlAttribute()]
        public string UnitOfMeasure;      
        [XmlElement("TaxAmount")]
        public TaxDetails taxdetails;
    }

    public class TaxDetails
    {
        [XmlAttribute()]
        public string Included;
        [XmlAttribute()]
        public string Amount;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM