簡體   English   中英

從具有特定屬性的C#類生成XML

[英]Generate XML from C# Class with specific attributes

我遇到了有關C#中類的XML序列化的問題:我想要的是以下格式的XML

<?xml version="1.0" encoding="utf-8"?>
<MyClass:Req xsi:schemaLocation="http://www.Test.com/MyClass.xsd"
            xmlns:TestClass="http://www.Test.com/MyClass" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://www.Test.com/MyClass">
  <head>
    <hA>01</hA>
  </head>
  <body>
    <R1>0</R1>
    <R3 />
    <R4 />
  </body>
</MyClass:Req>

所有XMLNS屬性都是​​必需的,並且需要通過代碼進行配置。 我當前得到的輸出是:

<?xml version="1.0" encoding="utf-8"?>
<MyClass_x003A_Req xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://www.Test.com/MyClass">
  <head>
    <hA>01</hA>
  </head>
  <body>
    <R1>0</R1>
    <R3 />
    <R4 />
  </body>
</MyClass_x003A_Req>

如您所見,包含序言的行不正確,我需要它僅顯示第一個XML示例中的值。 另外,冒號也被轉換為x003A ,我需要將其設為冒號。

這是我目前正在使用的代碼,請記住它只是測試代碼,這些字段將在生產前進行更改。

request theReq = new request();
requestHead rHead = new requestHead();
requestBody rBody = new requestBody();

rHead.hA = "01";

rBody.R1 = "0";

theReq.head = rHead;
theReq.body = rBody;

XmlSerializer serializer = new XmlSerializer(typeof(request));
TextWriter textWriter = new StreamWriter(@"Test.xml");
serializer.Serialize(textWriter, theReq);
textWriter.Close();




[XmlRoot(Namespace = "http://www.Test.com/MyClass",
    ElementName = "MyClass:Req",
    DataType = "string",
    IsNullable = true)]
public partial class request
{
    private requestHead headField;

    private requestBody bodyField;

    public request()
    {
        this.bodyField = new requestBody();
        this.headField = new requestHead();
    }

    public requestHead head
    {
        get
        {
            return this.headField;
        }
        set
        {
            this.headField = value;
        }
    }

    public requestBody body
    {
        get
        {
            return this.bodyField;
        }
        set
        {
            this.bodyField = value;
        }
    }
}

public partial class requestHead
{
    private string headAField;

    private string headBField;

    public string hA
    {
        get
        {
            return this.headAField;
        }
        set
        {
            this.headAField = value;
        }
    }

    public string hB
    {
        get
        {
            return this.headBField;
        }
        set
        {
            this.headBField = value;
        }
    }
}

public partial class requestBody
{
    private string R1Field;

    private string R2Field;

    private requestBodyR3 R3Field;

    private requestBodyR4 R4Field;

    public requestBody()
    {
        this.R4Field = new requestBodyR4();
        this.R3Field = new requestBodyR3();
    }

    public string R1
    {
        get
        {
            return this.R1Field;
        }
        set
        {
            this.R1Field = value;
        }
    }

    public string R2
    {
        get
        {
            return this.R2Field;
        }
        set
        {
            this.R2Field = value;
        }
    }

    public requestBodyR3 R3
    {
        get
        {
            return this.R3Field;
        }
        set
        {
            this.R3Field = value;
        }
    }

    public requestBodyR4 R4
    {
        get
        {
            return this.R4Field;
        }
        set
        {
            this.R4Field = value;
        }
    }
}

public partial class requestBodyR3
{

    private string R31Field;

    private string R32Field;

    public string R31
    {
        get
        {
            return this.R31Field;
        }
        set
        {
            this.R31Field = value;
        }
    }

    public string R32
    {
        get
        {
            return this.R32Field;
        }
        set
        {
            this.R32Field = value;
        }
    }
}

public partial class requestBodyR4
{
    private string R41Field;

    public string R41
    {
        get
        {
            return this.R41Field;
        }
        set
        {
            this.R41Field = value;
        }
    }
}

我知道我需要更改XMLRoot行以實現所需的輸出,但是我不確定在這里需要更改什么?

另外,我如何在將COLON寫入文件之前強制保留COLON,而不將其修改為x003A

最后一個問題,因為我沒有填充R3和R4,如果沒有填充值,就像沒有將R2和R4寫入文件一樣,我可以防止它們被寫入XML,我相信區別是R2是字符串以及R3和R4是復雜類型,使得序列化程序更難確定是否應寫入它們?

編輯:

應用Ralp的建議更改后,單擊確定,該文件現在如下所示:

<?xml version="1.0" encoding="utf-8"?>
<MyClass:Req d1p1:schemaLocation="http://www.test.com/myclass.xsd" 
             xmlns:d1p1="http://www.w3.org/2001/XMLSchema-instance" 
             xmlns:MyClass="http://www.test.com/myclass">
  <head>
    <hA>01</hA>
  </head>
  <body>
    <R1>0</R1>
    <R3 />
    <R4 />
  </body>
</MyClass:Req>

如何更改xsi的d1p1?

另外缺少的一行是xmlns =“ http://www.Test.com/MyClass”>,我該如何添加呢?

編輯-2:

幾乎有了Ralp的幫助,這是更新的文件:

<?xml version="1.0" encoding="utf-8"?>
<MyClass:Req xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://www.test.com/myclass.xsd" 
             xmlns:MyClass="http://www.test.com/myclass">
  <head>
    <hA>01</hA>
  </head>
  <body>
    <R1>0</R1>
    <R3 />
    <R4 />
  </body>
</MyClass:Req>

並更新了CODE:

request theReq = new request();
requestHead rHead = new requestHead();
requestBody rBody = new requestBody();

rHead.hA = "01";

rBody.R1 = "0";

theReq.head = rHead;
theReq.body = rBody;

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
ns.Add("MyClass", "http://www.test.com/myclass");

XmlSerializer serializer = new XmlSerializer(typeof(request));
TextWriter textWriter = new StreamWriter(@"Test.xml");
serializer.Serialize(textWriter, theReq, ns);
textWriter.Close();



[XmlRoot(Namespace = "http://www.test.com/myclass", ElementName = "Req")]
public partial class request
{
    private requestHead headField;

    private requestBody bodyField;

    public request()
    {
        this.bodyField = new requestBody();
        this.headField = new requestHead();
    }

    [XmlAttribute("schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string xsiSchemaLocation = "http://www.test.com/myclass.xsd";

    [XmlElement("head", Namespace = "")]
    public requestHead head
    {
        get
        {
            return this.headField;
        }
        set
        {
            this.headField = value;
        }
    }

    [XmlElement("body", Namespace = "")]
    public requestBody body
    {
        get
        {
            return this.bodyField;
        }
        set
        {
            this.bodyField = value;
        }
    }
}

public partial class requestHead
{
    private string headAField;

    private string headBField;

    public string hA
    {
        get
        {
            return this.headAField;
        }
        set
        {
            this.headAField = value;
        }
    }

    public string hB
    {
        get
        {
            return this.headBField;
        }
        set
        {
            this.headBField = value;
        }
    }
}

public partial class requestBody
{
    private string R1Field;

    private string R2Field;

    private requestBodyR3 R3Field;

    private requestBodyR4 R4Field;

    public requestBody()
    {
        this.R4Field = new requestBodyR4();
        this.R3Field = new requestBodyR3();
    }

    public string R1
    {
        get
        {
            return this.R1Field;
        }
        set
        {
            this.R1Field = value;
        }
    }

    public bool ShouldSerializeR1() { return !String.IsNullOrEmpty(R1); }

    public string R2
    {
        get
        {
            return this.R2Field;
        }
        set
        {
            this.R2Field = value;
        }
    }

    public bool ShouldSerializeR2() { return !String.IsNullOrEmpty(R2); }

    public requestBodyR3 R3
    {
        get
        {
            return this.R3Field;
        }
        set
        {
            this.R3Field = value;
        }
    }

    public bool ShouldSerializeR3() { return requestBodyR3.ShouldSerializeRequestBodyR3(); }

    public requestBodyR4 R4
    {
        get
        {
            return this.R4Field;
        }
        set
        {
            this.R4Field = value;
        }
    }
}

public partial class requestBodyR3
{

    private string R31Field;

    private string R32Field;

    public static bool ShouldSerializeRequestBodyR3()
    {
        bool blnRetVal = false;

        //Add code to test these values later

        return blnRetVal;
    }

    public string R31
    {
        get
        {
            return this.R31Field;
        }
        set
        {
            this.R31Field = value;
        }
    }

    public string R32
    {
        get
        {
            return this.R32Field;
        }
        set
        {
            this.R32Field = value;
        }
    }
}

public partial class requestBodyR4
{
    private string R41Field;

    public string R41
    {
        get
        {
            return this.R41Field;
        }
        set
        {
            this.R41Field = value;
        }
    }
}

現在唯一的問題是缺少xmlns =“ http://www.Test.com/MyClass”>

如果我將以下代碼添加到名稱空間部分:

ns.Add("", "http://www.test.com/Myclass");

如果大小寫與名稱空間MyClass相同,我會得到我需要的輸出,但是NOTICEi不得不改變字母之一的大小寫以獲取輸出,然后它就不會輸出。

提前致謝

命名空間為MyClass,元素名稱為Req。 因此,您需要定義命名空間。

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();            
ns.Add("MyClass", "http://www.test.com/myclass");

並調用帶有XmlSerializerNamespaces實例的Serialize重載。 然后,Request類本身就是一個名為Req的Element。

[XmlRoot(Namespace = "http://www.test.com/myclass", ElementName = "Req")]
public class Request
{
    [XmlElement("R1", Namespace = "")]
    public string R1 { get; set; }
    public bool ShouldSerializeR1()  { return !string.IsNullOrWhiteSpace(R1); }

    [XmlAttribute("schemaLocation", Namespace = "")]
    public string xsiSchemaLocation = "http://www.test.com/myclass.xsd";
}

可以通過聲明為Xml屬性的屬性來添加schemaLocation。 可以通過使用ShouldSerialize * MyLovelyPropertyName()*語法的方法來控制是否應序列化屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM