Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XML-Get元素值-C#_C#_Xml - Fatal编程技术网

XML-Get元素值-C#

XML-Get元素值-C#,c#,xml,C#,Xml,我试图找到一个元素值是否存在,然后在它下面得到一个元素值。i、 e <Reply> <customer id="1223"> <group>A</group> <class> <custclass>AB</custclass> <custval>1</custval> </class> <

我试图找到一个元素值是否存在,然后在它下面得到一个元素值。i、 e

<Reply>
   <customer id="1223">
      <group>A</group>
      <class>
        <custclass>AB</custclass>
        <custval>1</custval>
      </class>
      <class>
        <custclass>CD</custclass>
        <custval>2</custval>
      </class>
   </customer>
</Reply>

A.
AB
1.
光盘
2.
如果custclass=“CD”,我需要获取custval元素值。在C#“2”中,将其设置为字符串的最佳方法是什么?因此,我要查找是否存在custclass元素值“CD”,然后返回custval元素值2


感谢提供任何信息。

我们可以通过各种方式读取属性值-

方法1-使用
XmlDocument

XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
XmlNodeList xnl = doc.SelectNodes("/Reply/customer/class");

foreach (XmlNode node in xnl)
{
    if (node.ChildNodes[0].InnerText == "CD")
    {
        Console.WriteLine(node.ChildNodes[1].InnerText);
    }
}
var custval = xml.XPathSelectElement("Reply/customer/class[custclass='CD']/custval").Value;
Console.WriteLine(custval);
方法2-使用
XDcoument
LINQ

XDocument xml = XDocument.Load(xmlFile);
var result = xml.Root.DescendantsAndSelf("class")
        .Where(r => (string)r.Element("custclass").Value == "CD")
        .Select(s=> (string)s.Element("custval").Value).Single();
Console.WriteLine(result);
方法3-使用
XDocument
XPathSelectElement

XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
XmlNodeList xnl = doc.SelectNodes("/Reply/customer/class");

foreach (XmlNode node in xnl)
{
    if (node.ChildNodes[0].InnerText == "CD")
    {
        Console.WriteLine(node.ChildNodes[1].InnerText);
    }
}
var custval = xml.XPathSelectElement("Reply/customer/class[custclass='CD']/custval").Value;
Console.WriteLine(custval);
方法4-使用
XmlSerializer

使用&use
反序列化
创建C#类以将xml转换为对象

[XmlRoot(ElementName = "class")]
public class Class
{
    [XmlElement(ElementName = "custclass")]
    public string Custclass { get; set; }
    [XmlElement(ElementName = "custval")]
    public string Custval { get; set; }
}

[XmlRoot(ElementName = "customer")]
public class Customer
{
    [XmlElement(ElementName = "group")]
    public string Group { get; set; }
    [XmlElement(ElementName = "class")]
    public List<Class> Class { get; set; }
    [XmlAttribute(AttributeName = "id")]
    public string Id { get; set; }
}

[XmlRoot(ElementName = "Reply")]
public class Reply
{
    [XmlElement(ElementName = "customer")]
    public Customer Customer { get; set; }
}

static async System.Threading.Tasks.Task Main(string[] args)
{
    string xmlFile = @"xxxxxx.xml";
    using (StreamReader r = new StreamReader(xmlFile))
    {
        string xmlString = r.ReadToEnd();

        XmlSerializer ser = new XmlSerializer(typeof(Reply));

        using (TextReader reader = new StringReader(xmlString))
        {
            var result = (Reply)ser.Deserialize(reader);
            var custvalue = result.Customer.Class.Where(i => i.Custclass == "CD").Select(a => a.Custval).Single();
            Console.WriteLine(custvalue);
        }
    }
}
[XmlRoot(ElementName=“class”)]
公共课
{
[XmlElement(ElementName=“custclass”)]
公共字符串Custclass{get;set;}
[xmlement(ElementName=“custval”)]
公共字符串Custval{get;set;}
}
[XmlRoot(ElementName=“客户”)]
公共类客户
{
[xmlement(ElementName=“group”)]
公共字符串组{get;set;}
[xmlement(ElementName=“class”)]
公共列表类{get;set;}
[XmlAttribute(AttributeName=“id”)]
公共字符串Id{get;set;}
}
[XmlRoot(ElementName=“Reply”)]
公开课答覆
{
[xmlement(ElementName=“客户”)]
公共客户客户{get;set;}
}
静态异步System.Threading.Tasks.Task Main(字符串[]args)
{
字符串xmlFile=@“xxxxxx.xml”;
使用(StreamReader r=新的StreamReader(xmlFile))
{
字符串xmlString=r.ReadToEnd();
XmlSerializer ser=新的XmlSerializer(typeof(Reply));
使用(TextReader=new StringReader(xmlString))
{
var result=(Reply)序列反序列化(reader);
var custvalue=result.Customer.Class.Where(i=>i.Custclass==“CD”).Select(a=>a.Custval.Single();
控制台写入线(custvalue);
}
}
}

我们可以通过各种方式读取属性值-

方法1-使用
XmlDocument

XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
XmlNodeList xnl = doc.SelectNodes("/Reply/customer/class");

foreach (XmlNode node in xnl)
{
    if (node.ChildNodes[0].InnerText == "CD")
    {
        Console.WriteLine(node.ChildNodes[1].InnerText);
    }
}
var custval = xml.XPathSelectElement("Reply/customer/class[custclass='CD']/custval").Value;
Console.WriteLine(custval);
方法2-使用
XDcoument
LINQ

XDocument xml = XDocument.Load(xmlFile);
var result = xml.Root.DescendantsAndSelf("class")
        .Where(r => (string)r.Element("custclass").Value == "CD")
        .Select(s=> (string)s.Element("custval").Value).Single();
Console.WriteLine(result);
方法3-使用
XDocument
XPathSelectElement

XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
XmlNodeList xnl = doc.SelectNodes("/Reply/customer/class");

foreach (XmlNode node in xnl)
{
    if (node.ChildNodes[0].InnerText == "CD")
    {
        Console.WriteLine(node.ChildNodes[1].InnerText);
    }
}
var custval = xml.XPathSelectElement("Reply/customer/class[custclass='CD']/custval").Value;
Console.WriteLine(custval);
方法4-使用
XmlSerializer

使用&use
反序列化
创建C#类以将xml转换为对象

[XmlRoot(ElementName = "class")]
public class Class
{
    [XmlElement(ElementName = "custclass")]
    public string Custclass { get; set; }
    [XmlElement(ElementName = "custval")]
    public string Custval { get; set; }
}

[XmlRoot(ElementName = "customer")]
public class Customer
{
    [XmlElement(ElementName = "group")]
    public string Group { get; set; }
    [XmlElement(ElementName = "class")]
    public List<Class> Class { get; set; }
    [XmlAttribute(AttributeName = "id")]
    public string Id { get; set; }
}

[XmlRoot(ElementName = "Reply")]
public class Reply
{
    [XmlElement(ElementName = "customer")]
    public Customer Customer { get; set; }
}

static async System.Threading.Tasks.Task Main(string[] args)
{
    string xmlFile = @"xxxxxx.xml";
    using (StreamReader r = new StreamReader(xmlFile))
    {
        string xmlString = r.ReadToEnd();

        XmlSerializer ser = new XmlSerializer(typeof(Reply));

        using (TextReader reader = new StringReader(xmlString))
        {
            var result = (Reply)ser.Deserialize(reader);
            var custvalue = result.Customer.Class.Where(i => i.Custclass == "CD").Select(a => a.Custval).Single();
            Console.WriteLine(custvalue);
        }
    }
}
[XmlRoot(ElementName=“class”)]
公共课
{
[XmlElement(ElementName=“custclass”)]
公共字符串Custclass{get;set;}
[xmlement(ElementName=“custval”)]
公共字符串Custval{get;set;}
}
[XmlRoot(ElementName=“客户”)]
公共类客户
{
[xmlement(ElementName=“group”)]
公共字符串组{get;set;}
[xmlement(ElementName=“class”)]
公共列表类{get;set;}
[XmlAttribute(AttributeName=“id”)]
公共字符串Id{get;set;}
}
[XmlRoot(ElementName=“Reply”)]
公开课答覆
{
[xmlement(ElementName=“客户”)]
公共客户客户{get;set;}
}
静态异步System.Threading.Tasks.Task Main(字符串[]args)
{
字符串xmlFile=@“xxxxxx.xml”;
使用(StreamReader r=新的StreamReader(xmlFile))
{
字符串xmlString=r.ReadToEnd();
XmlSerializer ser=新的XmlSerializer(typeof(Reply));
使用(TextReader=new StringReader(xmlString))
{
var result=(Reply)序列反序列化(reader);
var custvalue=result.Customer.Class.Where(i=>i.Custclass==“CD”).Select(a=>a.Custval.Single();
控制台写入线(custvalue);
}
}
}

您编写的任何代码?因此,您希望看到所有具有custclass of CD的类叶的custvalue是什么?请使用XPAth尝试一些XDocument魔术。具体来说,XPAth
//customer/class[custclass='CD']]/custval/text()
将找到您想要的,当使用您编写的任何代码时?因此,您希望看到所有具有custclass of CD的类叶的custvalue是什么?请使用XPAth尝试一些XDocument魔术。具体来说,XPAth
//customer/class[custclass='CD']]/custval/text()
将在使用方法4时找到您想要的内容?@Hogan,Oops没有找到它。。现在添加谢谢这是一个巨大的帮助,我很感激。@user2208746,不客气。请将此标记为答案方法4发生了什么事?@Hogan,哎呀,没找到。。现在添加谢谢这是一个巨大的帮助,我很感激。@user2208746,不客气。请将此标记为答案