XML相同的节点但需要不同的id读取方法

时间:2017-02-02 06:53:21

标签: c# xml visual-studio xml-parsing

我需要一种简单的节点解析技术。我已经读过node.attribute和node.innertext。如果您了解Visual Studio C#.net。

,请提供帮助

这是我的代码:

<abc>
    <Limits>
        <Row>
            <LimitId>101</LimitId>
            <Max>10000</Max>
            <Current>0</Current>
        </Row>
        <Row>
            <LimitId>102</LimitId>
            <Max>6000</Max>
            <Current>0</Current>
        </Row>
        <Row>
            <LimitId>109</LimitId>
            <Max>25000</Max>
            <Current>0</Current>
        </Row>
        <Row>
            <LimitId>200</LimitId>
            <Max>45000</Max>
            <Current>0</Current>
        </Row>
    </Limits>
</abc>

2 个答案:

答案 0 :(得分:0)

这个小例子做了以下事情:

  1. 将xml字符串反序列化为类型化对象(LimitCollection)的实例
  2. 演示迭代所有限制,将其输出到控制台
  3. 通过LINQ方法使用它的ID查找特定的限制记录FirstOrDefault

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    
    namespace ConsoleApplication11
    {
        class Program
        {
            static void Main(string[] args)
            {
                string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
                <abc>
                   <Limits>
                        <Row>
                            <LimitId>101</LimitId>
                            <Max>10000</Max>
                            <Current>0</Current>
                        </Row>
                        <Row>
                            <LimitId>102</LimitId>
                            <Max>6000</Max>
                            <Current>0</Current>
                        </Row>
                        <Row>
                            <LimitId>109</LimitId>
                            <Max>25000</Max>
                            <Current>0</Current>
                        </Row>
                        <Row>
                            <LimitId>200</LimitId>
                            <Max>45000</Max>
                            <Current>0</Current>
                        </Row>
                    </Limits>
                </abc>";
    
                XmlSerializer d = new XmlSerializer(typeof(LimitCollection));
                LimitCollection deserializedXml = (LimitCollection)d.Deserialize(new StringReader(xml));
    
                Console.WriteLine("Iterating all limits:");
                foreach (Limit lim in deserializedXml.Limits)
                {
                    Console.WriteLine($"LimitId: {lim.LimitId}, Current: {lim.Current}, Max: {lim.Max}");
                }
    
                Console.WriteLine();
                Limit particularLimit = deserializedXml.Limits.FirstOrDefault(l => l.LimitId == 109);
                if (particularLimit != null)
                {
                    Console.WriteLine($"Particular limit: {particularLimit.LimitId}, Current: {particularLimit.Current}, Max: {particularLimit.Max}");
                }
    
                Console.WriteLine("Press any key to finish...");
                Console.ReadKey();
            }
        }
    
    
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false, ElementName = "abc")]
        public partial class LimitCollection
        {
    
            private Limit[] limitsField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlArrayItemAttribute("Row", IsNullable = false)]
            public Limit[] Limits
            {
                get
                {
                    return this.limitsField;
                }
                set
                {
                    this.limitsField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        public partial class Limit
        {
    
            private byte limitIdField;
    
            private ushort maxField;
    
            private byte currentField;
    
            /// <remarks/>
            public byte LimitId
            {
                get
                {
                    return this.limitIdField;
                }
                set
                {
                    this.limitIdField = value;
                }
            }
    
            /// <remarks/>
            public ushort Max
            {
                get
                {
                    return this.maxField;
                }
                set
                {
                    this.maxField = value;
                }
            }
    
            /// <remarks/>
            public byte Current
            {
                get
                {
                    return this.currentField;
                }
                set
                {
                    this.currentField = value;
                }
            }
        }
    
    }
    
  4. 当您在生产xml中有名称空间时,正如您在评论中更新的那样,您可以使用在我的另一个示例中略微修改的相同方法:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    
    namespace ConsoleApplication11
    {
        class Program
        {
            static void Main(string[] args)
            {
                string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    <env:Envelope xmlns:env=""w3.org/2003/05/soap-envelope"" xmlns:m0=""schemas/type.xsd"" xmlns:m1=""schemas/app.xsd"">
        <env:Body>
            <m1:GetLimitsRes > 
                <m1:Response Response=""1""> 
                    <m0:Limits> 
                        <m0:Row> 
                            <m0:LimitId>100</m0:LimitId> 
                            <m0:Max>10000</m0:Max> 
                            <m0:Current>0</m0:Current> 
                        </m0:Row> 
                        <m0:Row> 
                            <m0:LimitId>101</m0:LimitId> 
                            <m0:Max>10000</m0:Max> 
                            <m0:Current>0</m0:Current> 
                        </m0:Row> 
                    </m0:Limits> 
                </m1:Response>
            </m1:GetLimitsRes>
        </env:Body> 
    </env:Envelope>";
    
                XmlSerializer d = new XmlSerializer(typeof(Envelope));
                Envelope e = (Envelope)d.Deserialize(new StringReader(xml));
    
                Console.WriteLine("Iterating all limits:");
                foreach (var lim in e.Body.GetLimitsRes.Response.Limits)
                {
                    Console.WriteLine($"LimitId: {lim.LimitId}, Current: {lim.Current}, Max: {lim.Max}");
                }
    
                Console.WriteLine();
                LimitsRow particularLimit = e.Body.GetLimitsRes.Response.Limits.FirstOrDefault(l => l.LimitId == 101);
                if (particularLimit != null)
                {
                    Console.WriteLine($"Particular limit: {particularLimit.LimitId}, Current: {particularLimit.Current}, Max: {particularLimit.Max}");
                }
    
                Console.WriteLine("Press any key to finish...");
                Console.ReadKey();
            }
        }
    
    
        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "w3.org/2003/05/soap-envelope")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "w3.org/2003/05/soap-envelope", IsNullable = false)]
        public partial class Envelope
        {
    
            private EnvelopeBody bodyField;
    
            /// <remarks/>
            public EnvelopeBody Body
            {
                get
                {
                    return this.bodyField;
                }
                set
                {
                    this.bodyField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "w3.org/2003/05/soap-envelope")]
        public partial class EnvelopeBody
        {
    
            private GetLimitsRes getLimitsResField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Namespace = "schemas/app.xsd")]
            public GetLimitsRes GetLimitsRes
            {
                get
                {
                    return this.getLimitsResField;
                }
                set
                {
                    this.getLimitsResField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "schemas/app.xsd")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "schemas/app.xsd", IsNullable = false)]
        public partial class GetLimitsRes
        {
    
            private GetLimitsResResponse responseField;
    
            /// <remarks/>
            public GetLimitsResResponse Response
            {
                get
                {
                    return this.responseField;
                }
                set
                {
                    this.responseField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "schemas/app.xsd")]
        public partial class GetLimitsResResponse
        {
    
            private LimitsRow[] limitsField;
    
            private byte responseField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlArrayAttribute(Namespace = "schemas/type.xsd")]
            [System.Xml.Serialization.XmlArrayItemAttribute("Row", IsNullable = false)]
            public LimitsRow[] Limits
            {
                get
                {
                    return this.limitsField;
                }
                set
                {
                    this.limitsField = value;
                }
            }
    
            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public byte Response
            {
                get
                {
                    return this.responseField;
                }
                set
                {
                    this.responseField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "schemas/type.xsd")]
        public partial class LimitsRow
        {
    
            private byte limitIdField;
    
            private ushort maxField;
    
            private byte currentField;
    
            /// <remarks/>
            public byte LimitId
            {
                get
                {
                    return this.limitIdField;
                }
                set
                {
                    this.limitIdField = value;
                }
            }
    
            /// <remarks/>
            public ushort Max
            {
                get
                {
                    return this.maxField;
                }
                set
                {
                    this.maxField = value;
                }
            }
    
            /// <remarks/>
            public byte Current
            {
                get
                {
                    return this.currentField;
                }
                set
                {
                    this.currentField = value;
                }
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "schemas/type.xsd")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "schemas/type.xsd", IsNullable = false)]
        public partial class Limits
        {
    
            private LimitsRow[] rowField;
    
            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("Row")]
            public LimitsRow[] Row
            {
                get
                {
                    return this.rowField;
                }
                set
                {
                    this.rowField = value;
                }
            }
        }
    }
    

答案 1 :(得分:0)

尝试xml Linq anonymous

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;



namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("Row").Select(x => new {
                limitId = (int)x.Element("LimitId"),
                max = (int)x.Element("Max"),
                current = (int)x.Element("Current")
            }).ToList();
        }
    }

}