我见过一种仅使用 C# 类名读取和写入 XML 到磁盘的技术,而Attributes[]
.
这叫什么,以及如何将其应用于如下所示的数据集:
<?xml version="1.0"?>
<feedback>
<report_metadata>
<org_name>Yahoo! Inc.</org_name>
<email>postmaster@dmarc.yahoo.com</email>
<report_id>1340012067.929728</report_id>
<date_range>
<begin>1339891200</begin>
<end>1339977599 </end>
</date_range>
</report_metadata>
<policy_published>
<domain>company.com</domain>
<adkim>r</adkim>
<aspf>r</aspf>
<p>none</p>
<pct>100</pct>
</policy_published>
<record>
<row>
<source_ip>123.18.181.64</source_ip>
<count>1</count>
<policy_evaluated>
<disposition>none</disposition>
<dkim>fail</dkim>
<spf>fail</spf>
</policy_evaluated>
</row>
<identifiers>
<header_from>company.com</header_from>
</identifiers>
<auth_results>
<dkim>
<domain>company.com</domain>
<result>neutral</result>
</dkim>
<spf>
<domain>company.com</domain>
<result>softfail</result>
</spf>
</auth_results>
</record>
</feedback>
更新
我能够将该 XML 转换为类型安全的 C#:
我运行了这个命令来获取架构
C:\Temp>xsd test.xml /c /language:cs /out:c:\temp
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'c:\temp\test.xsd'.
然后运行此命令以获取 CS 文件
C:\Temp>xsd test.xsd /c /language:cs
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\Temp\test.cs'.
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.544
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class feedback
{
private object[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("policy_published", typeof(feedbackPolicy_published), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("record", typeof(feedbackRecord), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("report_metadata", typeof(feedbackReport_metadata), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class feedbackPolicy_published
{
private string domainField;
private string adkimField;
private string aspfField;
private string pField;
private string pctField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string domain
{
get
{
return this.domainField;
}
set
{
this.domainField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string adkim
{
get
{
return this.adkimField;
}
set
{
this.adkimField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string aspf
{
get
{
return this.aspfField;
}
set
{
this.aspfField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string p
{
get
{
return this.pField;
}
set
{
this.pField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string pct
{
get
{
return this.pctField;
}
set
{
this.pctField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class feedbackRecord
{
private feedbackRecordRow[] rowField;
private feedbackRecordIdentifiers[] identifiersField;
private feedbackRecordAuth_results[] auth_resultsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("row", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public feedbackRecordRow[] row
{
get
{
return this.rowField;
}
set
{
this.rowField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("identifiers", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public feedbackRecordIdentifiers[] identifiers
{
get
{
return this.identifiersField;
}
set
{
this.identifiersField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("auth_results", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public feedbackRecordAuth_results[] auth_results
{
get
{
return this.auth_resultsField;
}
set
{
this.auth_resultsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class feedbackRecordRow
{
private string source_ipField;
private string countField;
private feedbackRecordRowPolicy_evaluated[] policy_evaluatedField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string source_ip
{
get
{
return this.source_ipField;
}
set
{
this.source_ipField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string count
{
get
{
return this.countField;
}
set
{
this.countField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("policy_evaluated", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public feedbackRecordRowPolicy_evaluated[] policy_evaluated
{
get
{
return this.policy_evaluatedField;
}
set
{
this.policy_evaluatedField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class feedbackRecordRowPolicy_evaluated
{
private string dispositionField;
private string dkimField;
private string spfField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string disposition
{
get
{
return this.dispositionField;
}
set
{
this.dispositionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string dkim
{
get
{
return this.dkimField;
}
set
{
this.dkimField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string spf
{
get
{
return this.spfField;
}
set
{
this.spfField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class feedbackRecordIdentifiers
{
private string header_fromField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string header_from
{
get
{
return this.header_fromField;
}
set
{
this.header_fromField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class feedbackRecordAuth_results
{
private feedbackRecordAuth_resultsDkim[] dkimField;
private feedbackRecordAuth_resultsSpf[] spfField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("dkim", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public feedbackRecordAuth_resultsDkim[] dkim
{
get
{
return this.dkimField;
}
set
{
this.dkimField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("spf", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public feedbackRecordAuth_resultsSpf[] spf
{
get
{
return this.spfField;
}
set
{
this.spfField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class feedbackRecordAuth_resultsDkim
{
private string domainField;
private string resultField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string domain
{
get
{
return this.domainField;
}
set
{
this.domainField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string result
{
get
{
return this.resultField;
}
set
{
this.resultField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class feedbackRecordAuth_resultsSpf
{
private string domainField;
private string resultField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string domain
{
get
{
return this.domainField;
}
set
{
this.domainField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string result
{
get
{
return this.resultField;
}
set
{
this.resultField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class feedbackReport_metadata
{
private string org_nameField;
private string emailField;
private string report_idField;
private feedbackReport_metadataDate_range[] date_rangeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string org_name
{
get
{
return this.org_nameField;
}
set
{
this.org_nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string email
{
get
{
return this.emailField;
}
set
{
this.emailField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string report_id
{
get
{
return this.report_idField;
}
set
{
this.report_idField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("date_range", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public feedbackReport_metadataDate_range[] date_range
{
get
{
return this.date_rangeField;
}
set
{
this.date_rangeField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class feedbackReport_metadataDate_range
{
private string beginField;
private string endField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string begin
{
get
{
return this.beginField;
}
set
{
this.beginField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string end
{
get
{
return this.endField;
}
set
{
this.endField = value;
}
}
}