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
C# 按属性值搜索属性_C#_Xml - Fatal编程技术网

C# 按属性值搜索属性

C# 按属性值搜索属性,c#,xml,C#,Xml,我有以下XML文件: <?xml version="1.0" encoding="utf-8"?> <root> <Communication Id ="456"> <Person> Ivan Ivanov </Person> <Describtion> <Age> 16 </Age> <Place> Moscow </Pl

我有以下XML文件:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <Communication Id ="456">
    <Person> Ivan Ivanov </Person>
   <Describtion> 
          <Age> 16 </Age>
          <Place> Moscow </Place>
          <Key Name ="Language"> English </Key>
          <Key Name ="Profession"> Doctor </Key>
    </Describtion>  
  </Communication>

  <Communication Id ="1010">
    <Person> Petr Petrov </Person>
    <Describtion>
          <Age> 21 </Age>
          <Place> St.Peterburg </Place>
          <Key Name ="Language"> Français </Key>
          <Key Name ="Profession"> Ingeneer </Key>
    </Describtion>  
  </Communication>
</root>

伊万伊万诺夫
16
莫斯科
英语
医生
彼得罗夫
21
圣彼得堡
法国
工程师

有一个具有不同值的属性名称的键标记列表。此值确定标记之间的值将写入哪个变量。我如何为这样的搜索编写算法?

您可以这样搜索

xmlDoc.SelectNodes("root/Communication/Describtion/Key[@Name=\"Language\"][text()=\" English \"]")

你可以这样搜索

xmlDoc.SelectNodes("root/Communication/Describtion/Key[@Name=\"Language\"][text()=\" English \"]")

尝试使用
XmlDocument

public static string getNodeValue(XmlDocument xmldoc, string id, string key)
{
    return xmldoc.SelectSingleNode($"root/Communication[@Id='{id}']/Describtion/Key[@Name='{key}']")
                 .InnerText
                 .Trim();
}
用法

输出

English
Français

尝试使用
XmlDocument

public static string getNodeValue(XmlDocument xmldoc, string id, string key)
{
    return xmldoc.SelectSingleNode($"root/Communication[@Id='{id}']/Describtion/Key[@Name='{key}']")
                 .InnerText
                 .Trim();
}
用法

输出

English
Français

我使用了Xml Linq以及字典和IEquatable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader(FILENAME);
            reader.ReadLine();  //allow unicode characters
            XDocument doc = XDocument.Load(reader);

            List<Person> people = doc.Descendants("Communication").Select(x => new Person()
            {
                id = (int)x.Attribute("Id"),
                name = (string)x.Element("Person"),
                age = (int)x.Descendants("Age").FirstOrDefault(),
                place = (string)x.Descendants("Place").FirstOrDefault(),
                language = ((string)x.Descendants("Key").Where(y => (string)y.Attribute("Name") == "Language").FirstOrDefault()).Trim(),
                profession = ((string)x.Descendants("Key").Where(y => (string)y.Attribute("Name") == "Profession").FirstOrDefault()).Trim()
            }).ToList();

            Dictionary<Person, List<Person>> dict = people
                .GroupBy(x => x, y => y)
                .ToDictionary(x => x.Key, y => y.ToList());

            List<Person> results = dict[new Person() { language = "English", profession = "Doctor" }].ToList();

        }
    }
    public class Person : IEquatable<Person>
    {
        public int id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
        public string place { get; set; }
        public string language { get; set; }
        public string profession { get; set; }

        public Boolean Equals(Person other)
        {
            return
                (language == other.language) && (profession == other.profession);
        }
        public override int GetHashCode()
        {
            return (language + "^" + profession).GetHashCode() ;
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
StreamReader=新的StreamReader(文件名);
reader.ReadLine();//允许使用unicode字符
XDocument doc=XDocument.Load(读卡器);
列出人员=文档后代(“通信”)。选择(x=>newperson()
{
id=(int)x.Attribute(“id”),
name=(string)x.Element(“Person”),
age=(int)x.age(“age”).FirstOrDefault(),
place=(string)x.products(“place”).FirstOrDefault(),
language=((字符串)x.subjections(“Key”)。其中(y=>(字符串)y.Attribute(“Name”)==“language”).FirstOrDefault()).Trim(),
profession=((字符串)x.subjections(“Key”)。其中(y=>(字符串)y.Attribute(“Name”)==“profession”).FirstOrDefault()).Trim()
}).ToList();
字典dict=人
.GroupBy(x=>x,y=>y)
.ToDictionary(x=>x.Key,y=>y.ToList());
列表结果=dict[newperson(){language=“English”,profession=“Doctor”}.ToList();
}
}
公共类人士:IEquatable
{
公共int id{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
公共字符串place{get;set;}
公共字符串语言{get;set;}
公共字符串职业{get;set;}
公共布尔等于(其他人)
{
返回
(语言==其他.语言)和&(专业==其他.专业);
}
公共覆盖int GetHashCode()
{
return(language+“^”+profession).GetHashCode();
}
}
}

我使用了Xml Linq以及字典和IEquatable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            StreamReader reader = new StreamReader(FILENAME);
            reader.ReadLine();  //allow unicode characters
            XDocument doc = XDocument.Load(reader);

            List<Person> people = doc.Descendants("Communication").Select(x => new Person()
            {
                id = (int)x.Attribute("Id"),
                name = (string)x.Element("Person"),
                age = (int)x.Descendants("Age").FirstOrDefault(),
                place = (string)x.Descendants("Place").FirstOrDefault(),
                language = ((string)x.Descendants("Key").Where(y => (string)y.Attribute("Name") == "Language").FirstOrDefault()).Trim(),
                profession = ((string)x.Descendants("Key").Where(y => (string)y.Attribute("Name") == "Profession").FirstOrDefault()).Trim()
            }).ToList();

            Dictionary<Person, List<Person>> dict = people
                .GroupBy(x => x, y => y)
                .ToDictionary(x => x.Key, y => y.ToList());

            List<Person> results = dict[new Person() { language = "English", profession = "Doctor" }].ToList();

        }
    }
    public class Person : IEquatable<Person>
    {
        public int id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
        public string place { get; set; }
        public string language { get; set; }
        public string profession { get; set; }

        public Boolean Equals(Person other)
        {
            return
                (language == other.language) && (profession == other.profession);
        }
        public override int GetHashCode()
        {
            return (language + "^" + profession).GetHashCode() ;
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
StreamReader=新的StreamReader(文件名);
reader.ReadLine();//允许使用unicode字符
XDocument doc=XDocument.Load(读卡器);
列出人员=文档后代(“通信”)。选择(x=>newperson()
{
id=(int)x.Attribute(“id”),
name=(string)x.Element(“Person”),
age=(int)x.age(“age”).FirstOrDefault(),
place=(string)x.products(“place”).FirstOrDefault(),
language=((字符串)x.subjections(“Key”)。其中(y=>(字符串)y.Attribute(“Name”)==“language”).FirstOrDefault()).Trim(),
profession=((字符串)x.subjections(“Key”)。其中(y=>(字符串)y.Attribute(“Name”)==“profession”).FirstOrDefault()).Trim()
}).ToList();
字典dict=人
.GroupBy(x=>x,y=>y)
.ToDictionary(x=>x.Key,y=>y.ToList());
列表结果=dict[newperson(){language=“English”,profession=“Doctor”}.ToList();
}
}
公共类人士:IEquatable
{
公共int id{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
公共字符串place{get;set;}
公共字符串语言{get;set;}
公共字符串职业{get;set;}
公共布尔等于(其他人)
{
返回
(语言==其他.语言)和&(专业==其他.专业);
}
公共覆盖int GetHashCode()
{
return(language+“^”+profession).GetHashCode();
}
}
}

非常感谢@梅多克,你用两种语言让大家猜你需要什么。请将对您有用的内容标记为答案。非常感谢@梅多克,你用两种语言让大家猜你需要什么。请将对您有用的内容标记为答案。