jjzjj

Xelement

全部标签

c# - 为什么 XElement 没有 GetAttributeValue 方法?

有时我想知道某些API更改的原因。由于谷歌没有帮助我解决这个问题,也许StackOverflow可以。为什么Microsoft选择删除XML元素上的GetAttribute辅助方法?在System.Xml世界中有XmlElement.GetAttribute("x")就像它之前的MSXML中的getAttribute一样,两者都返回属性值或缺失时为空字符串。使用XElement有SetAttributeValue但GetAttributeValue没有实现。当然,修改逻辑以测试和使用XElement.Attribute("x").Value属性并没有太多工作,但它不是那么方便,并且以一

c# - 使用 LINQ 查找具有特定属性名称和值的 XElement

XDocumentxDocument=XDocument.Load("...");IEnumerableelements=xDocument.Element("lfm").Element("events").Elements("event");try{foreach(XElementelminelements){comm.Parameters.AddWithValue("extID",elm.Element("id").Value??"");comm.Parameters.AddWithValue("Title",elm.Element("title").Value??"");comm

c# - XMLSerializer 到 XElement

我一直在数据库LINQ中使用XML,发现使用序列化程序非常困难。数据库LINQ需要一个存储XElement的字段。我有一个包含许多自定义结构类的复杂对象,所以我想使用XmlSerializer来序列化该对象。但是,序列化器只能序列化到文件(“C:\xxx\xxx.xml”)或内存流。但是要将它转换或序列化为XElement以便我可以使用LINQ将其存储在数据库中吗?以及如何做相反的事情?即反序列化一个XElement... 最佳答案 尝试使用这个using(varstream=newMemoryStream()){serialize

c# - 如何使用 foreach 和 LINQ 构建 XDocument?

我可以使用XDocument构建以下工作正常的文件:XDocumentxdoc=newXDocument(newXDeclaration("1.0","utf-8",null),newXElement(_pluralCamelNotation,newXElement(_singularCamelNotation,newXElement("id","1"),newXElement("whenCreated","2008-12-31")),newXElement(_singularCamelNotation,newXElement("id","2"),newXElement("whenCr

c# - 使用 & 符号解析 XML

我有一个包含XML的字符串,我只想将它解析为Xelement,但它有一个&符号。我在使用HtmlDecode解析它时仍然遇到问题。有什么建议么?stringtest="";XElement.Parse(HttpUtility.HtmlDecode(test));我还添加了这些方法来替换那些字符,但我仍然收到XMLException。stringencodedXml=test.Replace("&","&").Replace("",">").Replace("\"",""").Replace("'","'");XElementmyXML=XElement

c# - 包含 namespace 的 XML 元素的 XDocument 或 XElement 解析

我尝试读取从log4netUdpAppender捕获的以下字符串。Logentry103当尝试XElement.Parse或XDocument.Parse内容时,它会抛出异常:'log4net'isanundeclarednamespace.Line1,position2.我知道我可以搜索并替换原始字符串中的“log4net:”并将其删除,这样我就可以成功解析XML,但有没有更好的方法?这是捕获的完整数据(重新格式化以供阅读),没有创建或删除xml命名空间声明。 最佳答案 首先,创建一个XmlNamespaceManager类的实例

c# - 将单个 XElement 转换为对象

我有一个看起来像这样的XElement:然后我有一个看起来像这样的类:publicclassProductAttribute{publicstringFlag{get;set;}publicstringSect{get;set;}publicstringHeader{get;set;}publicstringBody{get;set;}publicstringExtrainfo{get;set;}}如何将此XElement转换为ProductAttribute对象? 最佳答案 你必须在你的类和类成员上放置正确的序列化属性[Seria

c# - 如果 XElement 上不存在 XAttribute 默认值

如果XElement上的XAttribute不存在,是否有更简单/更好的方法来返回默认值?:我试着用更短的方式写这个(因为它是两行):vara=root.Attribute("testAttribute");varv=(a==null?"":a.Value);我的方法:通过扩展方法:publicstaticXAttributeAttribute(thisXElementobj,stringname,stringdefaultValue){if(obj.Attribute(name)==null)returnnewXAttribute(name,defaultValue);returno

c# - 我的 XDeclaration 在哪里?

出于某种原因,以下代码生成不包含声明的XML:XDocumentxDocument=newXDocument(newXDeclaration("1.0","utf-8","yes"),newXElement("project",newXAttribute("number",project.ProjectNumber),newXElement("store",newXAttribute("number",project.StoreNumber)),//UserElementnewXElement("user",newXAttribute("owner-id",project.OwnerI

c# - 如何获取 XML 或 XElement 变量中的特定元素计数

考虑这个XML:1000NimaAgha1001LighaLigha1002JighaJigha1003AbaAba我声明一个XElement变量并创建XML并将其分配给该变量。我如何在C#中获取此XML变量中的ID元素? 最佳答案 先决条件:为了使用.Count(),您需要导入命名空间System.Linq:usingSystem.Linq;您可以使用Descendantsmethod过滤后代元素名称为“ID”,然后统计结果:intcount=xml.Descendants("ID").Count();请注意,Descendant