【问题标题】:XElement get start Tag stringXElement 获取开始标记字符串
【发布时间】:2013-10-01 07:36:08
【问题描述】:

是否可以获取 XElement 的开始标签字符串?

例如,如果我有一个这样的 xml 元素

<Product Id="101" Name="Product 1">
    <Images>
        // ..
    </Images>
    <Description>
        // ..
    </Description>
</Product>

我只想获取开始标签:

<Product Id="101" Name="Product 1">

我将此用于验证反馈目的。

【问题讨论】:

  • 你用什么来遍历XML文档:XML阅读器,XPathNavigator .... ?
  • XDocument 是您在内存中存储数据的方式,但是您如何在文档中一一读取标签,或者您还没有这样做?
  • 要读取元素,我会这样做:(XDocument)xDocument.Element("root").Elements("Product"),它给了我XElement 对象的列表
  • 这可能与您的问题重复:using XSLT
  • 我希望有一个更简单的解决方法

标签: c# asp.net linq-to-xml xelement


【解决方案1】:

使用类似的查询

 XElement xele = XElement.Load("xmlfilename");
 XNamespace _XNamespace = XNamespace.Get("namespace url");
 IEnumerable<XElement> ProductAttribute = from ele in xele .Descendants(_XNamespace + "Product ")
                                          where ele.Attribute("Id").Value =="101" && ele.Attribute("Name") == "Product 1"
                                          select ele;

希望对你有用

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 2018-08-31
    • 2012-12-28
    • 1970-01-01
    • 2015-05-13
    相关资源
    最近更新 更多