Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在序列化XML中访问C#对象时出现问题_C#_.net_Xml_Xml Serialization - Fatal编程技术网

在序列化XML中访问C#对象时出现问题

在序列化XML中访问C#对象时出现问题,c#,.net,xml,xml-serialization,C#,.net,Xml,Xml Serialization,以下是来自XML架构的自动生成类的片段: [System.Xml.Serialization.XmlElementAttribute("PeriodEnd", typeof(string), DataType = "nonNegativeInteger")] [System.Xml.Serialization.XmlElementAttribute("PeriodEndYear", typeof(string), DataType = "nonNegativeInteger"

以下是来自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”和“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.SelectionStartDate = datevariable
注意,您不需要
部分。不确定在这种情况下硬编码是什么意思,但可以覆盖要硬编码的任何属性的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; }
    }
}
使用系统;
使用System.Xml;
使用System.Xml.Serialization;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
选择标准;
XmlSerializer serializer=新的XmlSerializer(typeof(SelectionCriteria));
//此字符串应该是要反序列化的元素
字符串xml=
"";
criteria=(SelectionCriteria)serializer.Deserialize(new System.IO.StringReader(xml));
}
}
[XmlRoot(“选择标准”)]
公共类选择标准
{
[xmldattribute(“PeriodEnd”)]
公共字符串PeriodEnd{get;set;}
[XmlAttribute(“PeriodEndYear”)]
公共字符串PeriodEndYear{get;set;}
[xmldattribute(“PeriodStart”)]
公共字符串PeriodStart{get;set;}
[xmltattribute(“PeriodStartYear”)]
公共字符串PeriodStartYear{get;set;}
[XmlAttribute(“SelectionEndDate”)]
public DateTime SelectionEndDate{get;set;}
[XmlAttribute(“SelectionStartDate”)]
公共日期时间选择开始日期{get;set;}
}
}

我以前也回答过类似的问题:

您也可以共享XML吗?当然,这是XML的标题部分,我正在尝试访问,谢谢您的及时回复。实际上我无法访问af.Header.SelectionCriteria.SelectionStartDate:/什么意思?这是你的密码;您可以[将]访问[级别设置为]任何内容。我不必触摸自动生成的代码af.Header.SelectionCriteria=new SelectionCriteriaStructure();af.Header.SelectionCriteria.ItemsElementName=new ItemsChoiceType1[]{ItemsChoiceType1.SelectionStartDate,ItemsChoiceType1.SelectionEndDate};af.Header.SelectionCriteria.Items=新对象[]{new DateTime(year,month,01),new DateTime(year,month,DateTime.DaysInMonth(year,month))};
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; }
    }
}