无法在序列化XML中访问C#对象

时间:2014-08-11 18:39:00

标签: c# .net xml xml-serialization

以下是来自XML架构的自动生成类的片段:

    [System.Xml.Serialization.XmlElementAttribute("PeriodEnd", typeof(string), DataType = "nonNegativeInteger")]  
    [System.Xml.Serialization.XmlElementAttribute("PeriodEndYear", typeof(string), DataType = "nonNegativeInteger")]  
    [System.Xml.Serialization.XmlElementAttribute("PeriodStart", typeof(string), DataType = "nonNegativeInteger")]  
    [System.Xml.Serialization.XmlElementAttribute("PeriodStartYear", typeof(string), DataType = "nonNegativeInteger")]  
    [System.Xml.Serialization.XmlElementAttribute("SelectionEndDate", typeof(System.DateTime), DataType = "date")]  
    [System.Xml.Serialization.XmlElementAttribute("SelectionStartDate", typeof(System.DateTime), DataType = "date")]  
    [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
    public object[] Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }

我的问题是,我需要硬编码" SelectionEndDate"和#34; SelectionStartDate"

自动生成的类创建了一个名为Items的对象数组。

在我的主程序中,我想做类似

的事情
af.Header.SelectionCriteria.Items.SelectionStartDate = datevariable

关于如何修改这些字段的任何建议?

xml看起来像这样

<Header>
        <AuditFileVersion>2.01</AuditFileVersion>
        <AuditFileCountry>LU</AuditFileCountry>
        <AuditFileDateCreated>2014-08-01</AuditFileDateCreated>
        <SoftwareCompanyName>MyCompany</SoftwareCompanyName>
        <SoftwareID>MyCompany</SoftwareID>
        <SoftwareVersion>2.0</SoftwareVersion>
        <Company>
            <RegistrationNumber>1234 567 891</RegistrationNumber>
            <Name>MyCompany</Name>
            <Address>
                <City>MyCity</City>
                <PostalCode>1234</PostalCode>
            </Address>
            <Contact>
                <ContactPerson>
                    <FirstName>John</FirstName>
                    <LastName>Doe</LastName>
                </ContactPerson>
                <Telephone />
            </Contact>
            <TaxRegistration>
                <TaxRegistrationNumber>XX12334</TaxRegistrationNumber>
            </TaxRegistration>
            <BankAccount>
                <IBANNumber>132456</IBANNumber>
            </BankAccount>
        </Company>
        <DefaultCurrencyCode>EUR</DefaultCurrencyCode>
        <SelectionCriteria>
            <SelectionStartDate>2014-07-01</SelectionStartDate>
            <SelectionEndDate>2014-07-31</SelectionEndDate>
        </SelectionCriteria>
        <TaxAccountingBasis>Invoice</TaxAccountingBasis>
    </Header>

使用

解决了问题
af.Header.SelectionCriteria = new SelectionCriteriaStructure();
            af.Header.SelectionCriteria.ItemsElementName = new ItemsChoiceType1[]{ItemsChoiceType1.SelectionStartDate,ItemsChoiceType1.SelectionEndDate};
            af.Header.SelectionCriteria.Items = new object[] { new DateTime(year, month, 01), new DateTime(year, month, DateTime.DaysInMonth(year,month)) };

1 个答案:

答案 0 :(得分:0)

这应该让你开始。您的用法更像是

af.Header.SelectionCriteria.SelectionStartDate = datevariable

请注意,您不需要Items部分。在这种情况下,不确定您的硬编码是什么意思,但您可以覆盖您希望硬编码的任何属性的get方法,因此无论XML如何,您始终都会获得相同的值。

using System;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            SelectionCriteria criteria;
            XmlSerializer serializer = new XmlSerializer(typeof(SelectionCriteria));
            // this string should be the element you want to deserialize
            string xml =
                "<SelectionCriteria " + 
                "PeriodEnd=\"1\" " +
                "PeriodEndYear=\"2\" " + 
                "PeriodStart=\"3\" " + 
                "PeriodStartYear=\"4\" " +
                "SelectionEndDate=\"2014-07-31\" " +
                "SelectionStartDate=\"2014-07-01\"/>";
            criteria = (SelectionCriteria)serializer.Deserialize(new System.IO.StringReader(xml));
        }

    }

    [XmlRoot("SelectionCriteria")]
    public class SelectionCriteria
    {
        [XmlAttribute("PeriodEnd")]
        public string PeriodEnd { get; set; }
        [XmlAttribute("PeriodEndYear")]
        public string PeriodEndYear { get; set; }
        [XmlAttribute("PeriodStart")]
        public string PeriodStart { get; set; }
        [XmlAttribute("PeriodStartYear")]
        public string PeriodStartYear { get; set; }
        [XmlAttribute("SelectionEndDate")]
        public DateTime SelectionEndDate { get; set; }
        [XmlAttribute("SelectionStartDate")]
        public DateTime SelectionStartDate { get; set; }
    }
}

我之前回答过类似的问题:How to change an xml file elements attributes in vb.net