【问题标题】:Get Element from XDocument & Edit Attribute从 XDocument 中获取元素并编辑属性
【发布时间】:2014-10-21 02:50:52
【问题描述】:
<GetPromotionByIdResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" MajorVersion="2" xmlns="http://fake.com/services">
    <Header>
        <Response ResponseCode="OK">
            <RequestID>1</RequestID>
        </Response>
    </Header>
    <Promotion PromotionId="5200" EffectiveDate="2014-10-10T00:00:00" ExpirationDate="2014-11-16T23:59:00" PromotionStatus="Active" PromotionTypeName="DefaultPromotion">
        <Description TypeCode="Long" Culture="en-AU">Promotion Description</Description>
    </Promotion>
</GetPromotionByIdResponse>

我正在尝试提取这个

<Promotion PromotionId="5200" EffectiveDate="2014-10-10T00:00:00" ExpirationDate="2014-11-16T23:59:00" PromotionStatus="Active" PromotionTypeName="DefaultPromotion">
        <Description TypeCode="Long" Culture="en-AU">Promotion Description</Description>
    </Promotion>

并将 PromotionId="5200" 转换为 PromotionId="XXX"

我可以使用下面的代码提取 元素,但不知道如何更改属性

XNamespace xmlResponseNamespace = xmlPromotionResponse.Root.GetDefaultNamespace();

        XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable());
        nsm.AddNamespace("def", xmlResponseNamespace.ToString());

        XElement xmlPromotionElement =
            xmlPromotionResponse
            .Descendants().SingleOrDefault(p => p.Name.LocalName == "Promotion");

【问题讨论】:

    标签: c# xml linq-to-xml xelement


    【解决方案1】:

    你可以试试这个方法:

    XNamespace ns = "http://fake.com/services";
    XElement xmlPromotionElement = xmlPromotionResponse.Descendants(ns+"Promotion")
                                                       .SingleOrDefault();
    xmlPromotionElement.Attribute("PromotionId").Value = "XXX";
    

    使用简单的XNamespace + local-name 来引用命名空间中的元素。然后您可以使用.Attribute() 方法从XElement 中获取XAttribute 并更改属性的值。

    【讨论】:

    • 完美运行。非常感谢。我更喜欢使用 XNamespace,而不是 namespacemanager……代码少得多。
    • XmlNamespaceManager 在使用 XPath 查询 XML 时使用(在这种情况下,我们使用 LINQ,而不是 XPath)。如果您删除涉及 XmlNamespaceManager 的 2 行代码,您的原始代码甚至应该可以工作。
    【解决方案2】:

    试试这个:它返回Promotion标签中所有属性的值。

     XNamespace ns1 = XNamespace.Get("http://fake.com/services");
     var readPromotion = from a in xx.Descendants(ns1 + "Promotion")
                                select new
                                {
                                    PromotionID = (string)a.Attribute("PromotionId"),
                                    EffectiveDate = (string)a.Attribute("EffectiveDate"),
                                    ExpirationDate = (string)a.Attribute("ExpirationDate"),
                                    PromotionStatus = (string)a.Attribute("PromotionStatus"),
                                    PromotionTypeName = (string)a.Attribute("PromotionTypeName"),
                                    Description = (string)a.Value
    
                                };
    
            foreach (var read in readPromotion)
            {
                // Read values
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 2017-09-24
      相关资源
      最近更新 更多