【问题标题】:How to Extract XML value from Exact Response如何从确切响应中提取 XML 值
【发布时间】:2021-04-23 11:31:00
【问题描述】:

我需要从此 XML 中获取 Description0, ItemCode 的值。

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="http://afasfs/services/Exact.Entity.REST.EG/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <id>http://afasfs/services/Exact.Entity.REST.EG/Item/</id>
    <title type="text">Item</title>
    <link rel="self" title="Item" href="Item" />
    <entry>
        <id>http://afasfs/services/Exact.Entity.REST.EG/Item('00X0')</id>
        <link rel="edit" title="Item" href="Item('00X0')" />
        <title />
        <content type="application/xml">
            <m:properties>
                <d:Description0>HEX S/DRILL SCREW NEO 14-10 X 75 C4 (PK100)</d:Description0>
                <d:ItemCode>0V0X0sdfA</d:ItemCode>
            </m:properties>
        </content>
    </entry>
</feed>

我已尝试执行以下操作,但它们都不起作用并出现此错误

“:”字符,十六进制值 0x3A,不能包含在名称中。

var itemElements = xDoc.Descendants("feed");
var itemElements = xDoc.Element("feed");
var itemElements = xDoc.Elements("feed");
var itemElements = xDoc.Root.Elements("feed");
var itemElements = xDoc.Elements("feed").Element("entry");

foreach (XElement elem in itemElements.Elements()){}

提前感谢您的帮助。

【问题讨论】:

标签: c# xml xml-parsing


【解决方案1】:

您可以使用 命名空间 来获取值:

1 - 声明所有Namespace

XNamespace xn = "http://www.w3.org/2005/Atom";
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";

2 - 查询以获取 Description0, ItemCode:

var properties = xDoc
    .Descendants(xn + "entry")
    .Descendants(m + "properties")
    .Select(p => new { Description = p.Element(d + "Description0")?.Value, ItemCode = p.Element(d + "ItemCode")?.Value })
    .FirstOrDefault();

3 - 所有代码:

string xml = @"
<feed xml:base=""http://afasfs/services/Exact.Entity.REST.EG/"" xmlns=""http://www.w3.org/2005/Atom"" xmlns:d=""http://schemas.microsoft.com/ado/2007/08/dataservices"" xmlns:m=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"">
    <id>http://afasfs/services/Exact.Entity.REST.EG/Item/</id>
    <title type=""text"">Item</title>
    <link rel=""self"" title=""Item"" href=""Item"" />
    <entry>
        <id>http://afasfs/services/Exact.Entity.REST.EG/Item('00X0')</id>
        <link rel=""edit"" title=""Item"" href=""Item('00X0')"" />
        <title />
        <content type=""application/xml"">
            <m:properties>
                <d:Description0>HEX S/DRILL SCREW NEO 14-10 X 75 C4 (PK100)</d:Description0>
                <d:ItemCode>0V0X0sdfA</d:ItemCode>
            </m:properties>
        </content>
    </entry>
</feed>";

XDocument xDoc = XDocument.Parse(xml);

XNamespace xn = "http://www.w3.org/2005/Atom";
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";

var properties = xDoc
    .Descendants(xn + "entry")
    .Descendants(m + "properties")
    .Select(p => new { Description = p.Element(d + "Description0")?.Value, ItemCode = p.Element(d + "ItemCode")?.Value })
    .FirstOrDefault();

Console.WriteLine($"Description0:{ properties.Description} , ItemCode:{properties.ItemCode}");

结果

Description0:HEX S/DRILL SCREW NEO 14-10 X 75 C4 (PK100) , ItemCode:0V0X0sdfA

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2017-05-08
    • 2021-11-29
    • 2021-07-29
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多