Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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# 避免XML文档中的冗余_C#_.net_Xml_Linq_Dataset - Fatal编程技术网

C# 避免XML文档中的冗余

C# 避免XML文档中的冗余,c#,.net,xml,linq,dataset,C#,.net,Xml,Linq,Dataset,我使用的是一种没有冗余的XML <person> <eye> <eye_info> <eye_color> blue </eye_color> </eye_info> </eye> <hair> <hair_info> <hair_color> blue &l

我使用的是一种没有冗余的XML

<person>
  <eye>
    <eye_info>
       <eye_color>
       blue
       </eye_color>
    </eye_info>
  </eye>
  <hair>
    <hair_info>
       <hair_color>
       blue
       </hair_color>
    </hair_info>
  </hair>
</person>
我确实意识到这不是最明智的做法,我现在遇到的这种情况也不是没有预料到的

现在,假设我必须阅读以下格式的xml:

<person>
  <eye>
    <info>
       <color>
       blue
       </color>
    </info>
  </eye>
  <hair>
    <info>
       <color>
       blue
       </color>
    </info>
  </hair>
</person>
这将是一个冗余,因为我只能用我以前的方法在XML中识别一个字段,而“消歧器”在上面三个级别

有没有一种切实可行的方法,可以在上述所有领域或至少几个领域的情况下,毫无差错地进入一个领域

-[编辑]-


我提出了另一个问题,询问如何使用linq获得某个节点。

从XML文档中查询数据有一整套标准。该标准称为XPath,C有一个实现。这不是现成的最简单的学习方法,但它是从XML中提取数据的最佳技术之一,非常值得学习

这就是一个例子

编辑:我建议您了解LINQ to XML,因为它更强大,但是如果您仍然需要XPath,那么您的特定问题看起来就像我在这台计算机上没有VS,所以我无法验证这一点

XPathDocument doc = new XPathDocument(new StringReader(xmlString));
XPathNavigator nav = doc.CreateNavigator();

// Compile a standard XPath expression
XPathExpression expr = nav.Compile("/person/eye/eye_info/eye_color");
expr = nav.Compile("/catalog/cd/price");
XPathNodeIterator iterator = nav.Select(expr);

// Iterate on the node set
while (iterator.MoveNext())
{
   XPathNavigator nav2 = iterator.Current.Clone();
   Console.WriteLine(nav2.Value);
}

您还可以使用Linq to XML System.XML.Linq名称空间,并像这样检索数据

string xml = @"<persons>
<person> 
  <eye> 
    <info> 
       <color>blue</color> 
    </info> 
  </eye> 
  <hair> 
    <info> 
       <color>blonde</color> 
    </info> 
  </hair> 
</person>
<person> 
  <eye> 
    <info> 
       <color>green</color> 
    </info> 
  </eye> 
  <hair> 
    <info> 
       <color>brown</color> 
    </info> 
  </hair> 
</person>
</persons>";

XDocument document = XDocument.Parse(xml);

var query = from person in document.Descendants("person")
            select new
            {
                EyeColor = person.Element("eye").Element("info").Element("color").Value,
                HairColor = person.Element("hair").Element("info").Element("color").Value
            };

foreach (var person in query)
    Console.WriteLine("{0}\t{1}", person.EyeColor, person.HairColor);

请记住,DataSet类只能理解可以转换为关系数据库形式的XML。特别是,它不会处理具有多个父表的单个子表。将信息元素同时作为person、hair和eye的子元素就是一个例子。

我会使用Linq to Xml,这很自然。谢谢,我在尝试使用它时遇到了一个问题。。检查更新我将尝试xpath!你能给我举个例子吗?查看问题中的更新,了解XPath的一般介绍。请查看。在.Net中,XPath可通过XmlNode对象及其子对象(即XmlDocument)上的SelectNode和SelectNodes方法获得。
XPathDocument doc = new XPathDocument(new StringReader(xmlString));
XPathNavigator nav = doc.CreateNavigator();

// Compile a standard XPath expression
XPathExpression expr = nav.Compile("/person/eye/eye_info/eye_color");
expr = nav.Compile("/catalog/cd/price");
XPathNodeIterator iterator = nav.Select(expr);

// Iterate on the node set
while (iterator.MoveNext())
{
   XPathNavigator nav2 = iterator.Current.Clone();
   Console.WriteLine(nav2.Value);
}
string xml = @"<persons>
<person> 
  <eye> 
    <info> 
       <color>blue</color> 
    </info> 
  </eye> 
  <hair> 
    <info> 
       <color>blonde</color> 
    </info> 
  </hair> 
</person>
<person> 
  <eye> 
    <info> 
       <color>green</color> 
    </info> 
  </eye> 
  <hair> 
    <info> 
       <color>brown</color> 
    </info> 
  </hair> 
</person>
</persons>";

XDocument document = XDocument.Parse(xml);

var query = from person in document.Descendants("person")
            select new
            {
                EyeColor = person.Element("eye").Element("info").Element("color").Value,
                HairColor = person.Element("hair").Element("info").Element("color").Value
            };

foreach (var person in query)
    Console.WriteLine("{0}\t{1}", person.EyeColor, person.HairColor);