[英]XMLSerialization - Custom root attributes
我正在研究一个项目,以将XML序列化并将其设置为Web服务。 根元素是高度定制的,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<eAction xmlns="http://www.action.org/standards/PC_Surety/action1/xml/" xmlns:cation="http://www.caction.org/standards/pc_go/xml/" xmlns:acme="http://www.ACME.org/standards/ACME1/xml/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
根有几个不同的名称空间,并且xsd属性不存在。
我一直在尝试解决如何添加额外的名称空间属性的问题,并且也许串行化不是解决问题的方法。
到目前为止,我有一个带有一组简单的要序列化的测试程序:
private void button1_Click(object sender, EventArgs e)
{
XmlSerializer xmlSerialize = new XmlSerializer(typeof(eAction));
TextWriter txtWriter = new StreamWriter(@"c:\actionout.xml");
eAction eActionItem = new eAction();
xmlSerialize.Serialize(txtWriter, eActionItem);
}
[XmlRoot(ElementName="eAction")]
public class eAction
{
public string Name = string.Empty;
public string Street1 = string.Empty;
public string Street2 = string.Empty;
public string City = string.Empty;
public string State = string.Empty;
public string PostalCode = string.Empty;
public OtherInformation OtherInformation = new OtherInformation();
}
public class OtherInformation
{
public string DateOfBirth = string.Empty;
public string SSN = string.Empty;
}
运行此命令将创建以下xml:
<?xml version="1.0" encoding="utf-8"?>
<eAction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name />
<Street1 />
<Street2 />
<City />
<State />
<PostalCode />
<OtherInformation>
<DateOfBirth />
<SSN />
</OtherInformation>
</eAction>
有没有添加我需要的属性的好方法?
编辑:接受的答案解决了问题。 还有一个问题-我还必须添加:
xsi:schemaLocation="http://www.action.org/standards/PC_Surety/action1/xml/ standardsFile.xsd"
是否可以将其添加为命名空间,但用xsi代替xmlns,还是可以使用其他方法添加此属性?
我认为这可能会满足您的需求。 你说得对,:)。
你的班:
[XmlRoot(ElementName = "eAction", Namespace = "http://www.action.org/standards/PC_Surety/action1/xml/")]
public class eAction
{
[XmlAttribute(Namespace = XmlSchema.InstanceNamespace)]
public string schemaLocation = "http://www.action.org/standards/PC_Surety/action1/xml/standardsFile.xsd";
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
public eAction()
{
xmlns.Add("cation", "http://www.caction.org/standards/pc_go/xml/");
xmlns.Add("acme", "http://www.ACME.org/standards/ACME1/xml/");
xmlns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
}
public string Name = string.Empty;
public string Street1 = string.Empty;
public string Street2 = string.Empty;
public string City = string.Empty;
public string State = string.Empty;
public string PostalCode = string.Empty;
public OtherInformation OtherInformation = new OtherInformation();
}
public class OtherInformation
{
public string DateOfBirth = string.Empty;
public string SSN = string.Empty;
}
使用示例:
private static void button1_click()
{
var xmlSerializer = new XmlSerializer(typeof(eAction));
using (var txtWriter = new StreamWriter(@"C:\temp\actionout.xml"))
{
var eActionItem = new eAction();
xmlSerializer.Serialize(txtWriter, eActionItem);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.