使用 C# 为 XmlSerializer 初始化 XML 成员 [英] XML members initialization for XmlSerializer using C#

查看:28
本文介绍了使用 C# 为 XmlSerializer 初始化 XML 成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML,需要反序列化以获取ThreadGroup.num_threads"、ThreadGroup.ramp_time"、HTTPSampler.path"和HTTPSampler.domain"的值.

I have below XML and need to deserialized to get the value of "ThreadGroup.num_threads","ThreadGroup.ramp_time","HTTPSampler.path" and "HTTPSampler.domain".

    <TestPlan>
        <hashTree>
            <hashTree>
                <ThreadGroup>
                    <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
                    <stringProp name="ThreadGroup.num_threads">10</stringProp>
                    <stringProp name="ThreadGroup.ramp_time">1</stringProp>
                    <longProp name="ThreadGroup.start_time">1517853259000</longProp>
                    <longProp name="ThreadGroup.end_time">1517853259000</longProp>
                    <boolProp name="ThreadGroup.scheduler">false</boolProp>
                    <stringProp name="ThreadGroup.duration"></stringProp>
                    <stringProp name="ThreadGroup.delay"></stringProp>
                </ThreadGroup>
                <hashTree>
                    <hashTree>
                        <HTTPSamplerProxy>
                            <stringProp name="HTTPSampler.domain">www.abc.com/abc-service-api</stringProp>
                            <stringProp name="HTTPSampler.port"></stringProp>
                            <stringProp name="HTTPSampler.protocol"></stringProp>
                            <stringProp name="HTTPSampler.contentEncoding"></stringProp>
                            <stringProp name="HTTPSampler.path">/v1/test/test?debug=false</stringProp>
                            <stringProp name="HTTPSampler.method">GET</stringProp>
                            <boolProp name="HTTPSampler.follow_redirects">false</boolProp>
                            <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
                            <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
                            <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
                            <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
                            <stringProp name="HTTPSampler.connect_timeout"></stringProp>
                            <stringProp name="HTTPSampler.response_timeout"></stringProp>
                        </HTTPSamplerProxy>
                    </hashTree>
                </hashTree>
            </hashTree>
        </hashTree>
    </TestPlan>

下面提到的我使用的代码.

The code I am using mentioned below.

    public class xmlData
        {
            [Serializable, XmlRoot("jmeterTestPlan")]
            public partial class jmeterTestPlan
            {
                private jmeterTestPlanHashTree hashTreeField;
                /// <remarks/>
                public jmeterTestPlanHashTree hashTree
                {
                    get
                    {
                        return this.hashTreeField;
                    }
                    set
                    {
                        this.hashTreeField = value;
                    }
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
            public partial class jmeterTestPlanHashTree
            {
                private jmeterTestPlanHashTreeHashTree hashTreeField;
                /// <remarks/>
                public jmeterTestPlanHashTreeHashTree hashTree
                {
                    get
                    {
                        return this.hashTreeField;
                    }
                    set
                    {
                        this.hashTreeField = value;
                    }
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
            public partial class jmeterTestPlanHashTreeHashTree
            {
                private jmeterTestPlanHashTreeHashTreeThreadGroup threadGroupField;
                private jmeterTestPlanHashTreeHashTreeHashTree hashTreeField;
                /// <remarks/>
                public jmeterTestPlanHashTreeHashTreeThreadGroup ThreadGroup
                {
                    get
                    {
                        return this.threadGroupField;
                    }
                    set
                    {
                        this.threadGroupField = value;
                    }
                }
                /// <remarks/>
                public jmeterTestPlanHashTreeHashTreeHashTree hashTree
                {
                    get
                    {
                        return this.hashTreeField;
                    }
                    set
                    {
                        this.hashTreeField = value;
                    }
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
            public partial class jmeterTestPlanHashTreeHashTreeThreadGroup
            {
                private object[] itemsField;
                /// <remarks/>
                [System.Xml.Serialization.XmlElementAttribute("boolProp", typeof(jmeterTestPlanHashTreeHashTreeThreadGroupBoolProp))]
                [System.Xml.Serialization.XmlElementAttribute("longProp", typeof(jmeterTestPlanHashTreeHashTreeThreadGroupLongProp))]
                [System.Xml.Serialization.XmlElementAttribute("stringProp", typeof(jmeterTestPlanHashTreeHashTreeThreadGroupStringProp))]
                public object[] Items
                {
                    get
                    {
                        return this.itemsField;
                    }
                    set
                    {
                        this.itemsField = value;
                    }
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
            public partial class jmeterTestPlanHashTreeHashTreeThreadGroupBoolProp
            {
                private string nameField;
                private bool valueField;
                /// <remarks/>
                [System.Xml.Serialization.XmlAttributeAttribute()]
                public string name
                {
                    get
                    {
                        return this.nameField;
                    }
                    set
                    {
                        this.nameField = value;
                    }
                }
                /// <remarks/>
                [System.Xml.Serialization.XmlTextAttribute()]
                public bool Value
                {
                    get
                    {
                        return this.valueField;
                    }
                    set
                    {
                        this.valueField = value;
                    }
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
            public partial class jmeterTestPlanHashTreeHashTreeThreadGroupLongProp
            {
                private string nameField;
                private ulong valueField;
                /// <remarks/>
                [System.Xml.Serialization.XmlAttributeAttribute()]
                public string name
                {
                    get
                    {
                        return this.nameField;
                    }
                    set
                    {
                        this.nameField = value;
                    }
                }
                /// <remarks/>
                [System.Xml.Serialization.XmlTextAttribute()]
                public ulong Value
                {
                    get
                    {
                        return this.valueField;
                    }
                    set
                    {
                        this.valueField = value;
                    }
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
            public partial class jmeterTestPlanHashTreeHashTreeThreadGroupStringProp
            {
                private string nameField;
                private string valueField;
                /// <remarks/>
                [System.Xml.Serialization.XmlAttributeAttribute()]
                public string name
                {
                    get
                    {
                        return this.nameField;
                    }
                    set
                    {
                        this.nameField = value;
                    }
                }
                /// <remarks/>
                [System.Xml.Serialization.XmlTextAttribute()]
                public string Value
                {
                    get
                    {
                        return this.valueField;
                    }
                    set
                    {
                        this.valueField = value;
                    }
                }
            }
            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
            public partial class jmeterTestPlanHashTreeHashTreeHashTree
            {
                private jmeterTestPlanHashTreeHashTreeHashTreeHashTree hashTreeField;
                /// <remarks/>
                public jmeterTestPlanHashTreeHashTreeHashTreeHashTree hashTree
                {
                    get
                    {
                        return this.hashTreeField;
                    }
                    set
                    {
                        this.hashTreeField = value;
                    }
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
            public partial class jmeterTestPlanHashTreeHashTreeHashTreeHashTree
            {
                private jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxy hTTPSamplerProxyField;
                /// <remarks/>
                public jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxy HTTPSamplerProxy
                {
                    get
                    {
                        return this.hTTPSamplerProxyField;
                    }
                    set
                    {
                        this.hTTPSamplerProxyField = value;
                    }
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
            public partial class jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxy
            {
                private object[] itemsField;
                /// <remarks/>
                [System.Xml.Serialization.XmlElementAttribute("boolProp", typeof(jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxyBoolProp))]
                [System.Xml.Serialization.XmlElementAttribute("stringProp", typeof(jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxyStringProp))]
                public object[] Items
                {
                    get
                    {
                        return this.itemsField;
                    }
                    set
                    {
                        this.itemsField = value;
                    }
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
            public partial class jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxyBoolProp
            {
                private string nameField;
                private bool valueField;
                /// <remarks/>
                [System.Xml.Serialization.XmlAttributeAttribute()]
                public string name
                {
                    get
                    {
                        return this.nameField;
                    }
                    set
                    {
                        this.nameField = value;
                    }
                }
                /// <remarks/>
                [System.Xml.Serialization.XmlTextAttribute()]
                public bool Value
                {
                    get
                    {
                        return this.valueField;
                    }
                    set
                    {
                        this.valueField = value;
                    }
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
            public partial class jmeterTestPlanHashTreeHashTreeHashTreeHashTreeHTTPSamplerProxyStringProp
            {
                private string nameField;
                private string valueField;
                /// <remarks/>
                [System.Xml.Serialization.XmlAttributeAttribute()]
                public string name
                {
                    get
                    {
                        return this.nameField;
                    }
                    set
                    {
                        this.nameField = value;
                    }
                }
                /// <remarks/>
                [System.Xml.Serialization.XmlTextAttribute()]
                public string Value
                {
                    get
                    {
                        return this.valueField;
                    }
                    set
                    {
                        this.valueField = value;
                    }
                }
            }


        }

上面的代码给出了需要的值和很多额外的属性值,代码看起来也很长.但是,我需要这 4 个值.请提出更好的解决方案.

the above code is giving required value with a lot of additional attributes value and the code seems lengthy too. However, I need those 4 value. Please suggest any better solution.

推荐答案

UPDATED (2018-09-10)

我已经更新了它以使其易于使用.

UPDATED (2018-09-10)

I've updated this to get it easy to use.

在我开始之前,我认为您可能希望在 HTTPSamplerProxy 部分中使用 longProps 的可能性.它还使编码(更)更容易和更清晰.我已经在没有任何 longProps 的情况下对其进行了测试,以确保它以您期望的方式与现有 XML 配合使用.

Before I started, I decided that you probably wanted the possibility of longProps in the HTTPSamplerProxy section. It also made the coding (much) easier and cleaner. I've tested it without any longProps just to make sure it worked with the existing XML the way you'd expect.

对原始描述的更新为斜体

我所做的是使用标准的 XSD.exe 工具获取源 XML 文件(在 HTTPSamplerProxy 部分有一个额外的 longProp)并创建一个 XSD.然后我再次使用 XSD.exe 创建一个(非常难看的)C# 文件.在这一点上,整个混乱的根源是 TestPlan 类.这是 XSD.exe 生成的内容(更新 - 对新发出的 XSD.exe 代码的唯一更改是顶部的命名空间声明):

What I did was to use the standard XSD.exe tool to take your source XML file (with an extra longProp in the HTTPSamplerProxy section) and create an XSD. Then I used XSD.exe again to create a (very ugly) C# file. At that point, the root of the whole mess is the TestPlan class. Here's what XSD.exe produced (updated - the only change to the newly-emitted XSD.exe code was a namespace declaration at the top):

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[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 hashTree {

    private hashTreeHTTPSamplerProxy[] hTTPSamplerProxyField;

    private hashTree[] hashTree1Field;

    private hashTreeThreadGroup[] threadGroupField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("HTTPSamplerProxy", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public hashTreeHTTPSamplerProxy[] HTTPSamplerProxy {
        get {
            return this.hTTPSamplerProxyField;
        }
        set {
            this.hTTPSamplerProxyField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("hashTree")]
    public hashTree[] hashTree1 {
        get {
            return this.hashTree1Field;
        }
        set {
            this.hashTree1Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("ThreadGroup", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public hashTreeThreadGroup[] ThreadGroup {
        get {
            return this.threadGroupField;
        }
        set {
            this.threadGroupField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class hashTreeHTTPSamplerProxy {

    private longProp[] longPropField;

    private stringProp[] stringPropField;

    private boolProp[] boolPropField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("longProp", IsNullable = true)]
    public longProp[] longProp {
        get {
            return this.longPropField;
        }
        set {
            this.longPropField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("stringProp", IsNullable = true)]
    public stringProp[] stringProp {
        get {
            return this.stringPropField;
        }
        set {
            this.stringPropField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("boolProp", IsNullable = true)]
    public boolProp[] boolProp {
        get {
            return this.boolPropField;
        }
        set {
            this.boolPropField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public partial class longProp {

    private string nameField;

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public partial class stringProp {

    private string nameField;

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public partial class boolProp {

    private string nameField;

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class hashTreeThreadGroup {

    private stringProp[] stringPropField;

    private longProp[] longPropField;

    private boolProp[] boolPropField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("stringProp", IsNullable = true)]
    public stringProp[] stringProp {
        get {
            return this.stringPropField;
        }
        set {
            this.stringPropField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("longProp", IsNullable = true)]
    public longProp[] longProp {
        get {
            return this.longPropField;
        }
        set {
            this.longPropField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("boolProp", IsNullable = true)]
    public boolProp[] boolProp {
        get {
            return this.boolPropField;
        }
        set {
            this.boolPropField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[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 TestPlan {

    private object[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("boolProp", typeof(boolProp), IsNullable = true)]
    [System.Xml.Serialization.XmlElementAttribute("hashTree", typeof(hashTree))]
    [System.Xml.Serialization.XmlElementAttribute("longProp", typeof(longProp), IsNullable = true)]
    [System.Xml.Serialization.XmlElementAttribute("stringProp", typeof(stringProp), IsNullable = true)]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

剩下的大部分都是新的

XSD.exe 发出的代码创建了一组部分类.我在一个单独的源文件中以几种方式扩展了部分类.但是,在开始之前,我声明了一个接口和一个静态工作类.

The rest of this is mostly new

The code emitted by XSD.exe creates a set of partial classes. I extended the partial classes in a few ways in a separate source file. But, before I started, I declared an interface and a static worker class.

public interface IGrouping {
    boolProp[] boolProp { get; }
    stringProp[] stringProp { get; }
    longProp[] longProp { get; }

    Dictionary<string, bool?> BoolProperties { get; }
    Dictionary<string, long?> LongProperties { get; }
    Dictionary<string, string> StringProperties { get; }
}

public static class PropertyGrouper  {
    public static void GroupProperties(IGrouping itemToGroup) {
        //has this been done before, if yes, return
        if (itemToGroup.StringProperties.Count > 0 || itemToGroup.BoolProperties.Count > 0 || itemToGroup.LongProperties.Count > 0 ) {
            return;
        }
        //otherwise
        if (itemToGroup.boolProp != null) {
            foreach (var bProp in itemToGroup.boolProp) {
                var succeeded = bool.TryParse(bProp.Value, out var bValue);
                itemToGroup.BoolProperties.Add(bProp.name, succeeded ? bValue : (bool?)null);
            }
        }
        if (itemToGroup.longProp != null) {
            foreach (var lProp in itemToGroup.longProp) {
                var succeeded = long.TryParse(lProp.Value, out var lValue);
                itemToGroup.LongProperties.Add(lProp.name, succeeded ? lValue : (long?)null);
            }
        }
        if (itemToGroup.stringProp != null) {
            foreach (var sProp in itemToGroup.stringProp) {
                itemToGroup.StringProperties.Add(sProp.name, sProp.Value);
            }
        }
    }
}

有了这些,我扩展了每个 XSD.exe 发出的类.TestPlan 类很简单 - 我只是添加了一个类型化的属性:

With those in place, I extended each of the XSD.exe-emitted classes. The TestPlan class was easy - I just added a typed property:

public partial class TestPlan {
    [XmlIgnore]
    public hashTree HashTree => Items[0] as hashTree;
}

hashTreeThreadGrouphashTreeHTTPSamplerProxy 类以相同的方式扩展(记住,我没有命名这些类中的任何一个,XSD.exe 从你的 XML 命名它们).每个都被声明为实现 IGrouping 接口,每个都获得了 3 个属性字典(根据 IGrouping 的要求).IGrouping 接口中的三个数组位于 XSD 发出的代码中:

The hashTreeThreadGroup and hashTreeHTTPSamplerProxy classes were extended in the same way (remember, I didn't name any of these classes, XSD.exe named them from your XML) . Each was declared to implement the IGrouping interface, and each got 3 dictionaries of properties (as required by IGrouping). The three arrays in the IGrouping interface were in the XSD-emitted code:

public partial class hashTreeThreadGroup : IGrouping {
    [XmlIgnore]
    public Dictionary<string, bool?> BoolProperties { get; } = new Dictionary<string, bool?>();
    [XmlIgnore]
    public Dictionary<string, long?> LongProperties { get; } = new Dictionary<string, long?>();
    [XmlIgnore]
    public Dictionary<string, string> StringProperties { get; } = new Dictionary<string, string>();
}

public partial class hashTreeHTTPSamplerProxy : IGrouping {
    [XmlIgnore]
    public Dictionary<string, bool?> BoolProperties { get; } = new Dictionary<string, bool?>();
    [XmlIgnore]
    public Dictionary<string, long?> LongProperties { get; } = new Dictionary<string, long?>();
    [XmlIgnore]
    public Dictionary<string, string> StringProperties { get; } = new Dictionary<string, string>();
}

最后,我扩展了 hashTree 类.我添加了三个类型化的属性,每个属性都有一个空检查来使事情变得干净.ThreadGroupItem 和 HttpSamplerProxyItem 属性分别调用 PropertyGrouper.GroupProperties.第一次调用时,XmlSerializer 创建的属性数组中的属性被复制到 Dictionaries 中.

Finally, I extended hashTree class. I added three typed properties, each with a null check to make things clean. The ThreadGroupItem and HttpSamplerProxyItem properties each get a call to PropertyGrouper.GroupProperties. The first time this is called, the properties in the XmlSerializer-created property arrays are copied into Dictionaries.

public partial class hashTree {
    [XmlIgnore]
    public hashTree HashTree {
        get {
            if (hashTree1 != null) {
                return hashTree1[0] as hashTree;
            } else {
                return null;
            }
        }
    }


    [XmlIgnore]
    public hashTreeThreadGroup ThreadGroupItem {
        get {
            if (ThreadGroup != null) {
                var threadGroup = ThreadGroup[0]; // as hashTreeThreadGroup;
                PropertyGrouper.GroupProperties(threadGroup);
                return threadGroup;
            } else {
                return null;
            }
        }
    }

    [XmlIgnore]
    public hashTreeHTTPSamplerProxy HttpSamplerProxyItem {
        get {
            if (HTTPSamplerProxy != null) {
                var httpSamplerProxy = HTTPSamplerProxy[0];
                PropertyGrouper.GroupProperties(httpSamplerProxy);
                return httpSamplerProxy;
            } else {
                return null;
            }
        }
    }
}

一切就绪后,我运行了相同的代码.

With that all in place, I ran the same code.

   TestPlan result;
   using (var stream = new FileStream("source.xml", FileMode.Open, FileAccess.Read)) {
       var serializer = new XmlSerializer(typeof(TestPlan));
       result = (TestPlan)serializer.Deserialize(stream);
   }

这段代码是我如何通过我的第一个示例访问您的数据.

This code is how I accessed your data with my first example.

 var numThreadsParsed = long.TryParse((((XmlSerializeForm.hashTree)result.Items[0]).hashTree1[0].ThreadGroup[0].stringProp[1].Value), out var numThreads);
 var httpSamplerPath = ((XmlSerializeForm.hashTree)result.Items[0]).hashTree1[0].hashTree1[0].hashTree1[0].HTTPSamplerProxy[0].stringProp[4].Value;

但是,通过我做的一些简单的添加(嗯,代码并不复杂,但正确的是),访问属性更干净:

But, with the few simple additions I made (well, the code isn't that complicated, but getting it right was), accessing the properties is much cleaner:

 string numThreadsParsed = result.HashTree.HashTree.ThreadGroupItem.StringProperties["ThreadGroup.num_threads"];
 long? startTime = result.HashTree.HashTree.ThreadGroupItem.LongProperties["ThreadGroup.start_time"];
 string httpSamplerPath = result.HashTree.HashTree.HashTree.HashTree.HttpSamplerProxyItem.StringProperties["HTTPSampler.path"];
 bool? useKeepAlive = result.HashTree.HashTree.HashTree.HashTree.HttpSamplerProxyItem.BoolProperties["HTTPSampler.use_keepalive"];

你去吧!

这篇关于使用 C# 为 XmlSerializer 初始化 XML 成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆