使用 C# 为 XmlSerializer 初始化 XML 成员

我有以下 XML,需要反序列化以获取“ThreadGroup.num_threads”、“ThreadGroup.ramp_time”、“HTTPSampler.path”和“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>

                        </HTTPSamplerProxy>

                    </hashTree>

                </hashTree>

            </hashTree>

        </hashTree>

    </TestPlan>

上面的代码给出了具有许多附加属性值的所需值,并且代码似乎也很长。但是,我需要这 4 个值。请提出任何更好的解决方案。


慕尼黑的夜晚无繁华
浏览 291回答 3
3回答

Helenr

我已经更新了它以使其易于使用。我开始之前,我决定,你可能想的可能性longProps在HTTPSamplerProxy部分。它还使编码(很多)更容易和更清洁。我已经在没有任何 longProps 的情况下对其进行了测试,只是为了确保它以您期望的方式与现有 XML 一起工作。原流程对原始描述的更新以斜体显示我所做的是使用标准的 XSD.exe 工具获取您的源 XML 文件(在 HTTPSamplerProxy 部分中有一个额外的 longProp)并创建一个 XSD。然后我再次使用 XSD.exe 创建了一个(非常丑陋的)C# 文件。那时,整个混乱的根源是 TestPlan 类。这是 XSD.exe 生成的内容(更新 - 对新发出的 XSD.exe 代码的唯一更改是顶部的命名空间声明):/// <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 {&nbsp; &nbsp; private hashTreeHTTPSamplerProxy[] hTTPSamplerProxyField;&nbsp; &nbsp; private hashTree[] hashTree1Field;&nbsp; &nbsp; private hashTreeThreadGroup[] threadGroupField;&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("HTTPSamplerProxy", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]&nbsp; &nbsp; public hashTreeHTTPSamplerProxy[] HTTPSamplerProxy {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.hTTPSamplerProxyField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.hTTPSamplerProxyField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("hashTree")]&nbsp; &nbsp; public hashTree[] hashTree1 {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.hashTree1Field;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.hashTree1Field = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("ThreadGroup", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]&nbsp; &nbsp; public hashTreeThreadGroup[] ThreadGroup {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.threadGroupField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.threadGroupField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}/// <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 {&nbsp; &nbsp; private longProp[] longPropField;&nbsp; &nbsp; private stringProp[] stringPropField;&nbsp; &nbsp; private boolProp[] boolPropField;&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("longProp", IsNullable = true)]&nbsp; &nbsp; public longProp[] longProp {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.longPropField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.longPropField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("stringProp", IsNullable = true)]&nbsp; &nbsp; public stringProp[] stringProp {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.stringPropField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.stringPropField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("boolProp", IsNullable = true)]&nbsp; &nbsp; public boolProp[] boolProp {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.boolPropField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.boolPropField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}/// <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 {&nbsp; &nbsp; private string nameField;&nbsp; &nbsp; private string valueField;&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlAttributeAttribute()]&nbsp; &nbsp; public string name {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.nameField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.nameField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlTextAttribute()]&nbsp; &nbsp; public string Value {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.valueField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.valueField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}/// <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 {&nbsp; &nbsp; private string nameField;&nbsp; &nbsp; private string valueField;&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlAttributeAttribute()]&nbsp; &nbsp; public string name {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.nameField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.nameField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlTextAttribute()]&nbsp; &nbsp; public string Value {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.valueField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.valueField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}/// <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 {&nbsp; &nbsp; private string nameField;&nbsp; &nbsp; private string valueField;&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlAttributeAttribute()]&nbsp; &nbsp; public string name {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.nameField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.nameField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlTextAttribute()]&nbsp; &nbsp; public string Value {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.valueField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.valueField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}/// <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 {&nbsp; &nbsp; private stringProp[] stringPropField;&nbsp; &nbsp; private longProp[] longPropField;&nbsp; &nbsp; private boolProp[] boolPropField;&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("stringProp", IsNullable = true)]&nbsp; &nbsp; public stringProp[] stringProp {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.stringPropField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.stringPropField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("longProp", IsNullable = true)]&nbsp; &nbsp; public longProp[] longProp {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.longPropField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.longPropField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("boolProp", IsNullable = true)]&nbsp; &nbsp; public boolProp[] boolProp {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.boolPropField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.boolPropField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}/// <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 {&nbsp; &nbsp; private object[] itemsField;&nbsp; &nbsp; /// <remarks/>&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("boolProp", typeof(boolProp), IsNullable = true)]&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("hashTree", typeof(hashTree))]&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("longProp", typeof(longProp), IsNullable = true)]&nbsp; &nbsp; [System.Xml.Serialization.XmlElementAttribute("stringProp", typeof(stringProp), IsNullable = true)]&nbsp; &nbsp; public object[] Items {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.itemsField;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.itemsField = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}其余的大部分是新的XSD.exe 发出的代码创建了一组部分类。我在一个单独的源文件中以几种方式扩展了部分类。但是,在开始之前,我声明了一个接口和一个静态工作类。public interface IGrouping {&nbsp; &nbsp; boolProp[] boolProp { get; }&nbsp; &nbsp; stringProp[] stringProp { get; }&nbsp; &nbsp; longProp[] longProp { get; }&nbsp; &nbsp; Dictionary<string, bool?> BoolProperties { get; }&nbsp; &nbsp; Dictionary<string, long?> LongProperties { get; }&nbsp; &nbsp; Dictionary<string, string> StringProperties { get; }}和public static class PropertyGrouper&nbsp; {&nbsp; &nbsp; public static void GroupProperties(IGrouping itemToGroup) {&nbsp; &nbsp; &nbsp; &nbsp; //has this been done before, if yes, return&nbsp; &nbsp; &nbsp; &nbsp; if (itemToGroup.StringProperties.Count > 0 || itemToGroup.BoolProperties.Count > 0 || itemToGroup.LongProperties.Count > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //otherwise&nbsp; &nbsp; &nbsp; &nbsp; if (itemToGroup.boolProp != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var bProp in itemToGroup.boolProp) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var succeeded = bool.TryParse(bProp.Value, out var bValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemToGroup.BoolProperties.Add(bProp.name, succeeded ? bValue : (bool?)null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (itemToGroup.longProp != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var lProp in itemToGroup.longProp) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var succeeded = long.TryParse(lProp.Value, out var lValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemToGroup.LongProperties.Add(lProp.name, succeeded ? lValue : (long?)null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (itemToGroup.stringProp != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var sProp in itemToGroup.stringProp) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemToGroup.StringProperties.Add(sProp.name, sProp.Value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}有了这些,我扩展了每个 XSD.exe 发出的类。这TestPlan门课很简单——我只是添加了一个类型化的属性:public partial class TestPlan {&nbsp; &nbsp; [XmlIgnore]&nbsp; &nbsp; public hashTree HashTree => Items[0] as hashTree;}在hashTreeThreadGroup与hashTreeHTTPSamplerProxy类相同的方式(请记住,我没有点名任何这些类的,XSD.EXE从XML命名他们)进行了扩展。每个都被声明为实现IGrouping接口,并且每个都有 3 个属性字典(根据 的要求IGrouping)。IGrouping接口中的三个数组位于XSD 发出的代码中:public partial class hashTreeThreadGroup : IGrouping {&nbsp; &nbsp; [XmlIgnore]&nbsp; &nbsp; public Dictionary<string, bool?> BoolProperties { get; } = new Dictionary<string, bool?>();&nbsp; &nbsp; [XmlIgnore]&nbsp; &nbsp; public Dictionary<string, long?> LongProperties { get; } = new Dictionary<string, long?>();&nbsp; &nbsp; [XmlIgnore]&nbsp; &nbsp; public Dictionary<string, string> StringProperties { get; } = new Dictionary<string, string>();}public partial class hashTreeHTTPSamplerProxy : IGrouping {&nbsp; &nbsp; [XmlIgnore]&nbsp; &nbsp; public Dictionary<string, bool?> BoolProperties { get; } = new Dictionary<string, bool?>();&nbsp; &nbsp; [XmlIgnore]&nbsp; &nbsp; public Dictionary<string, long?> LongProperties { get; } = new Dictionary<string, long?>();&nbsp; &nbsp; [XmlIgnore]&nbsp; &nbsp; public Dictionary<string, string> StringProperties { get; } = new Dictionary<string, string>();}最后,我延长了hashTree课程。我添加了三个类型化的属性,每个都有一个空检查以使事情变得干净。ThreadGroupItem 和 HttpSamplerProxyItem 属性分别调用PropertyGrouper.GroupProperties. 第一次调用时,XmlSerializer 创建的属性数组中的属性被复制到字典中。public partial class hashTree {&nbsp; &nbsp; [XmlIgnore]&nbsp; &nbsp; public hashTree HashTree {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (hashTree1 != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return hashTree1[0] as hashTree;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; [XmlIgnore]&nbsp; &nbsp; public hashTreeThreadGroup ThreadGroupItem {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ThreadGroup != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var threadGroup = ThreadGroup[0]; // as hashTreeThreadGroup;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyGrouper.GroupProperties(threadGroup);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return threadGroup;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; [XmlIgnore]&nbsp; &nbsp; public hashTreeHTTPSamplerProxy HttpSamplerProxyItem {&nbsp; &nbsp; &nbsp; &nbsp; get {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (HTTPSamplerProxy != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var httpSamplerProxy = HTTPSamplerProxy[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyGrouper.GroupProperties(httpSamplerProxy);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return httpSamplerProxy;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}一切就绪后,我运行了相同的代码。&nbsp; &nbsp;TestPlan result;&nbsp; &nbsp;using (var stream = new FileStream("source.xml", FileMode.Open, FileAccess.Read)) {&nbsp; &nbsp; &nbsp; &nbsp;var serializer = new XmlSerializer(typeof(TestPlan));&nbsp; &nbsp; &nbsp; &nbsp;result = (TestPlan)serializer.Deserialize(stream);&nbsp; &nbsp;}此代码是我使用第一个示例访问您的数据的方式。&nbsp;var numThreadsParsed = long.TryParse((((XmlSerializeForm.hashTree)result.Items[0]).hashTree1[0].ThreadGroup[0].stringProp[1].Value), out var numThreads);&nbsp;var httpSamplerPath = ((XmlSerializeForm.hashTree)result.Items[0]).hashTree1[0].hashTree1[0].hashTree1[0].HTTPSamplerProxy[0].stringProp[4].Value;但是,通过我做了一些简单的添加(好吧,代码并没有那么复杂,但是做对了),访问属性就更简洁了:&nbsp;string numThreadsParsed = result.HashTree.HashTree.ThreadGroupItem.StringProperties["ThreadGroup.num_threads"];&nbsp;long? startTime = result.HashTree.HashTree.ThreadGroupItem.LongProperties["ThreadGroup.start_time"];&nbsp;string httpSamplerPath = result.HashTree.HashTree.HashTree.HashTree.HttpSamplerProxyItem.StringProperties["HTTPSampler.path"];&nbsp;bool? useKeepAlive = result.HashTree.HashTree.HashTree.HashTree.HttpSamplerProxyItem.BoolProperties["HTTPSampler.use_keepalive"];给你!

芜湖不芜

使用外部 lib Cinchoo ETL - 一个开源库,您可以轻松获取所选节点值,如下所示定义 .NET 类型public class TestPlan{&nbsp; &nbsp; [ChoXmlNodeRecordField(XPath = @"/ThreadGroup/stringProp[@name=""ThreadGroup.on_sample_error""]")]&nbsp; &nbsp; public string NumThreads { get; set; }&nbsp; &nbsp; [ChoXmlNodeRecordField(XPath = @"/ThreadGroup/stringProp[@name=""ThreadGroup.ramp_time""]")]&nbsp; &nbsp; public int RampTime { get; set; }&nbsp; &nbsp; [ChoXmlNodeRecordField(XPath = @"/hashTree/hashTree/HTTPSamplerProxy/stringProp[@name=""HTTPSampler.path""]")]&nbsp; &nbsp; public string Path { get; set; }&nbsp; &nbsp; [ChoXmlNodeRecordField(XPath = @"/hashTree/hashTree/HTTPSamplerProxy/stringProp[@name=""HTTPSampler.domain""]")]&nbsp; &nbsp; public string Domain { get; set; }}然后使用 Cinchoo ETL 反序列化输入 xml,如下所示static void Main(){&nbsp; &nbsp; using (var p = new ChoXmlReader<TestPlan>("*** XML file path ***")&nbsp; &nbsp; &nbsp; &nbsp; .WithXPath("/TestPlan/hashTree/hashTree")&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach (var rec in p)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(rec.Dump());&nbsp; &nbsp; }}输出:-- ChoXmlReaderTest.Program+TestPlan State --&nbsp; &nbsp; &nbsp; &nbsp; NumThreads: continue&nbsp; &nbsp; &nbsp; &nbsp; RampTime: 1&nbsp; &nbsp; &nbsp; &nbsp; Path: /v1/test/test?debug=false&nbsp; &nbsp; &nbsp; &nbsp; Domain: www.abc.com/abc-service-api希望能帮助到你。免责声明:我是这个库的作者。

天涯尽头无女友

使用 xml linq :using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.Xml.Linq;namespace ConsoleApplication1{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; const string FILENAME = @"c:\temp\test.xml";&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XmlData data = new XmlData(FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class XmlData&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public int? num_threads { get; set;}&nbsp; &nbsp; &nbsp; &nbsp; public int? ramp_time { get;set;}&nbsp; &nbsp; &nbsp; &nbsp; List<SamplerProxy> HTTPSamplerProxies { get;set;}&nbsp; &nbsp; &nbsp; &nbsp; public XmlData(string filename)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XDocument doc = XDocument.Load(filename);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XElement threadGroup = doc.Descendants("ThreadGroup").FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_threads = (int?)threadGroup.Elements("stringProp").Where(x => (string)x.Attribute("name") == "ThreadGroup.num_threads").FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ramp_time = (int?)threadGroup.Elements("stringProp").Where(x => (string)x.Attribute("name") == "ThreadGroup.ramp_time").FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HTTPSamplerProxies = doc.Descendants("HTTPSamplerProxy").Select(x => new SamplerProxy() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path = (string)x.Elements("stringProp").Where(y => (string)y.Attribute("name") == "HTTPSampler.path").FirstOrDefault(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; domain = (string)x.Elements("stringProp").Where(y => (string)y.Attribute("name") == "HTTPSampler.domain").FirstOrDefault()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class SamplerProxy&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string path { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string domain { get; set; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP