C# XMLSerialization-自定义根属性

C# XMLSerialization-自定义根属性,c#,.net,xmlserializer,C#,.net,Xmlserializer,我正在做一个项目来序列化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.

我正在做一个项目来序列化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">
运行此命令将创建以下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替换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);
        }
    }

可能重复的谢谢!这非常清楚,帮助解决了我的问题。我在原来的问题上补充了一些东西。我想知道是否可以添加一个不以xmlns开头的名称空间属性。我还需要添加xsi:schemaNamespace.Oh,嘿。如果您有更多的事情要做(课程),您可能需要考虑使用:
[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);
        }
    }