XPath在c#中返回null,但在XPath验证程序中有效

时间:2019-03-06 20:36:34

标签: c# validation xpath choetl

这是我第一次使用XPath。

这是我的XML:

<content type="application/xml">
    <m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
        <d:Guid>YOIYOI-HNON-OIN</d:Guid>
        <d:ObjectId>6000009251</d:ObjectId>
        <d:ProcessType>ZMIN</d:ProcessType>
        <d:ProcessTypeTxt>Incident</d:ProcessTypeTxt>
        <d:Description>Test 2</d:Description>
        <d:IntroText>Incident</d:IntroText>
        <d:CreatedAtDateFormatted>08.05.18</d:CreatedAtDateFormatted>
        <d:ChangedAtDateFormatted>08.05.18</d:ChangedAtDateFormatted>
        <d:PostingDate>2018-05-08T00:00:00</d:PostingDate>
        <d:ChangedAtDate>2018-05-08T00:00:00</d:ChangedAtDate>
        <d:Priority>2</d:Priority>
        <d:PriorityTxt>2: High</d:PriorityTxt>
        <d:PriorityState>None</d:PriorityState>
        <d:Concatstatuser>New</d:Concatstatuser>
        <d:ActionRequired>false</d:ActionRequired>
        <d:StillOpen>true</d:StillOpen>
        <d:Icon></d:Icon>
        <d:SoldToPartyName></d:SoldToPartyName>
        <d:ServiceTeamName></d:ServiceTeamName>
        <d:PersonRespName></d:PersonRespName>
        <d:ConfigItemTxt></d:ConfigItemTxt>
    </m:properties>
</content>

还有其他内容节点。

我需要检索特定的标签值,例如:

  • d:Guid
  • d:ProcessType
  • d:说明
  • 依此类推。

但是我不需要所有标签值。

我尝试了一个在线Xpath验证器,在其中发布了XML并使用了以下表达式:

//content/m:properties/d:Guid | //content/m:properties/d:ObjectId

这给了我所需的数据,但是当我在c#应用程序中使用它时,它返回null。有人可以向我解释为什么会这样吗?还有,除了使用XPath之外,还有其他方法吗?

这是我的C#代码:

string xml = System.IO.File.ReadAllText(startupPath);
StringBuilder sb = new StringBuilder();

using (var node = ChoXmlReader.LoadText(xml).WithXPath("//content/m:properties/d:Guid or //content/m:properties/d:ObjectId"))
{
    using (var w = new ChoCSVWriter(sb).WithFirstLineHeader())
    {
        w.Write(node);
    }
}

Console.WriteLine(sb.ToString());
Console.ReadLine();

3 个答案:

答案 0 :(得分:0)

在在线验证器中,您使用|,但在代码中使用or。 ChoETL是否真的改变了XPaths的工作方式?我在文档中找不到证据。

此外,我认为在线工具会自动推断出名称空间前缀,但是您需要告诉ChoETL名称空间:

.WithXNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices")

和类似的

答案 1 :(得分:0)

您可以为此XML文件创建模型,然后将其序列化为对象,然后访问所需的属性。这是您可以使用的模型:

[XmlRoot(ElementName = "properties", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")]
    public class Properties
    {
        [XmlElement(ElementName = "Guid", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string Guid { get; set; }
        [XmlElement(ElementName = "ObjectId", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string ObjectId { get; set; }
        [XmlElement(ElementName = "ProcessType", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string ProcessType { get; set; }
        [XmlElement(ElementName = "ProcessTypeTxt", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string ProcessTypeTxt { get; set; }
        [XmlElement(ElementName = "Description", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string Description { get; set; }
        [XmlElement(ElementName = "IntroText", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string IntroText { get; set; }
        [XmlElement(ElementName = "CreatedAtDateFormatted", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string CreatedAtDateFormatted { get; set; }
        [XmlElement(ElementName = "ChangedAtDateFormatted", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string ChangedAtDateFormatted { get; set; }
        [XmlElement(ElementName = "PostingDate", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string PostingDate { get; set; }
        [XmlElement(ElementName = "ChangedAtDate", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string ChangedAtDate { get; set; }
        [XmlElement(ElementName = "Priority", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string Priority { get; set; }
        [XmlElement(ElementName = "PriorityTxt", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string PriorityTxt { get; set; }
        [XmlElement(ElementName = "PriorityState", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string PriorityState { get; set; }
        [XmlElement(ElementName = "Concatstatuser", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string Concatstatuser { get; set; }
        [XmlElement(ElementName = "ActionRequired", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string ActionRequired { get; set; }
        [XmlElement(ElementName = "StillOpen", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string StillOpen { get; set; }
        [XmlElement(ElementName = "Icon", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string Icon { get; set; }
        [XmlElement(ElementName = "SoldToPartyName", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string SoldToPartyName { get; set; }
        [XmlElement(ElementName = "ServiceTeamName", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string ServiceTeamName { get; set; }
        [XmlElement(ElementName = "PersonRespName", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string PersonRespName { get; set; }
        [XmlElement(ElementName = "ConfigItemTxt", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices")]
        public string ConfigItemTxt { get; set; }
        [XmlAttribute(AttributeName = "m", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string M { get; set; }
        [XmlAttribute(AttributeName = "d", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string D { get; set; }
    }

    [XmlRoot(ElementName = "content")]
    public class Content
    {
        [XmlElement(ElementName = "properties", Namespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata")]
        public Properties Properties { get; set; }
        [XmlAttribute(AttributeName = "type")]
        public string Type { get; set; }
    }

这是C#代码,可将其转换为对象:

XmlSerializer serializer = new XmlSerializer(typeof(Content));
 Content resultingMessage = (Content)serializer.Deserialize(new XmlTextReader(@"XMLFile1.xml"));

这是访问guid属性的代码:

string guid = resultingMessage.Properties.Guid;

希望这可以解决您的问题。

PS:我已经基于给定的XML文件创建了模型,如果对此XML schema进行了更改,则需要相应地修改model

快乐的编码...

答案 2 :(得分:0)

以下是使用Cinchoo ETL将选择性xml节点输出到csv文件的方法

var nsManager = new XmlNamespaceManager(new NameTable());
//register mapping of prefix to namespace uri 
nsManager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
nsManager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");

StringBuilder csv = new StringBuilder();
using (var p = ChoXmlReader.LoadText(xml)
        .WithXPath("//entry/content/m:properties")
        .WithXmlNamespaceManager(nsManager)
        .WithField("Guid", xPath: "d:Guid")
        .WithField("ProcessType", xPath: "d:ProcessType")
        .WithField("Description", xPath: "d:Description")
    )
{
    using (var w = new ChoCSVWriter(csv)
        .WithFirstLineHeader()
        )
        w.Write(p);
}

Console.WriteLine(csv);