Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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序列化DataContract类的XPath匹配_C#_Xml_Serialization_Xpath_Match - Fatal编程技术网

C# XML序列化DataContract类的XPath匹配

C# XML序列化DataContract类的XPath匹配,c#,xml,serialization,xpath,match,C#,Xml,Serialization,Xpath,Match,我有一个DataContract类MyDataContract。我正在将其序列化为XML文件。后来,在我的应用程序中的一个完全不同的“地方”,我正在加载这个文件。如果文件内容符合特殊条件,我只想验证是否加载。假设我存储了一个人及其与该人车辆的关联(假设一个人只能拥有一辆车辆):-) 这里是DataContract类: namespace Test.DataContracts { [DataContract] public class MyDataContract { [Dat

我有一个
DataContract
MyDataContract
。我正在将其序列化为XML文件。后来,在我的应用程序中的一个完全不同的“地方”,我正在加载这个文件。如果文件内容符合特殊条件,我只想验证是否加载。假设我存储了一个人及其与该人车辆的关联(假设一个人只能拥有一辆车辆):-)

这里是
DataContract
类:

namespace Test.DataContracts
{
  [DataContract]
  public class MyDataContract
  {
    [DataMember]
    public string Identifier { get; set; }

    [DataMember]
    public Common.Person Person { get; set; }

    [DataMember]
    public Common.Vehicle Vehicle { get; set; }
  }
}

namespace Test.DataContracts.Common
{
  [DataContract]
  public class Person
  {
    [DataMember]
    public Global.Gender Gender { get; set; }

    [DataMember]
    public string Info { get; set; }

    [DataMember]
    public string Name { get; set; }
  }

  [DataContract]
  public class Vehicle
  {
    [DataMember]
    public string Info { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Vendor { get; set; }
  }
}

namespace Test.DataContracts.Global
{
  [DataContract]
  public class Gender
  {
    [DataMember]
    public int Type { get; set; }

    [DataMember]
    public string Name { get; set; }
  }
}
产生以下序列化XML:

<?xml version="1.0" encoding="utf-8"?>
<MyDataContract xmlns="http://schemas.datacontract.org/2004/07/Test.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Identifier>123456789</Identifier>
  <Person xmlns:a="http://schemas.datacontract.org/2004/07/Test.DataContracts.Common">
    <a:Gender xmlns:b="http://schemas.datacontract.org/2004/07/Test.DataContracts.Global">
      <b:Name>Female</b:Name>
      <b:Type>0</b:Type>
    </a:Gender>
    <a:Info>She is a beautiful lady.</a:Info>
    <a:Name>Jane Doe</a:Name>
  </Person>
  <Vehicle xmlns:a="http://schemas.datacontract.org/2004/07/Test.DataContracts.Common">
    <a:Info>A super great car.</a:Info>
    <a:Name>Mustang 1983 Turbo GT</a:Name>
    <a:Vendor>Ford</a:Vendor>
  </Vehicle>
</MyDataContract>
有人能帮忙吗


更新1-我已使用XmlNamespaceManager更改了代码。但在执行
xNavigator.Matches(exp1)
时仍然会导致
false
,如果您有XDocument,则使用LINQ到XML更容易:

var xdoc = XDocument.Load(memorystream);
// Making it simple, grab the first
var type = xdoc.Descendants(XName.Get("Type","http://schemas.datacontract.org/2004/07/Test.DataContracts.Global")).FirstOrDefault();
var vendor = xdoc.Descendants(XName.Get("Vendor", "http://schemas.datacontract.org/2004/07/Test.DataContracts.Common")).FirstOrDefault();
string path = "blah";
if (type != null && type.Value == "0" && vendor != null && vendor.Value == "Ford")
{
    Console.WriteLine("File '{0}' indicates a female person that owns a vehicle of Ford.", path);
}
else
{
    Console.WriteLine("File '{0}' has no matches (female and Ford).", path);
}
如果您确定XPath是您需要的唯一解决方案:

using System.Xml.XPath;

var document = XDocument.Load(fileName);
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("a", "http://schemas.datacontract.org/2004/07/Test.DataContracts.Global");
var name = document.XPathSelectElement("path", namespaceManager).Value;

您必须使用XPath进行搜索有什么原因吗?您可以反序列化数据并使用Linq检查条件。您必须考虑名称空间。Ryan:是的,我需要XPath,因为我的程序是第三方,实际上不知道对象(这只是一个示例)。谢谢你的提示,我会调查的!
using System.Xml.XPath;

var document = XDocument.Load(fileName);
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("a", "http://schemas.datacontract.org/2004/07/Test.DataContracts.Global");
var name = document.XPathSelectElement("path", namespaceManager).Value;