xelement

Checking an XElement for Existence of One of Several Possible XElements

匆匆过客 提交于 2019-12-08 06:52:55
问题 Is there a way to determine if an XElement contains one of any specified elements? For example, I have XElements that I'll want to check: Dim xe1 = <color><blue/></color> Dim xe2 = <color><red/></color> Dim xe3 = <color><powderBlue/></color> Dim xe4 = <color><aqua/></color> Dim xe5 = <color><green/></color> I'd like to be able to query any of the xelements to see if they containt the elements <red/> , <green/> or <blue/> below them and return true if so, false if not. I was hoping that it

How can i check if this XElement is not null before adding it to an anonymous object?

心已入冬 提交于 2019-12-08 01:12:45
问题 I'm populating an anonymous object from an XML file. Up until now, commentary.Elements("Commentator") has always had a value so i've never had to check for null. I've had to remove that though, and now it's failing when it tries to read that line. I'm looking at the code and I don't know what to change though, because its being selected into a property of an anonymous object. var genericOfflineFactsheet = new { Commentary = (from commentary in doc.Elements("Commentary") select new {

Partially deserialize XML to Object

馋奶兔 提交于 2019-12-07 11:58:53
问题 I have some XML that I deserialize into a business object. I am using XmlSerializer.Deserialize to do so. However, I want one of the XmlElement contained in the XML to stay an XElement. It cannot be done directly (using an XmlElementAttribute) since XElement is not Serializable. I also tried to serialize that element to a string (in a two steps attempt to get an XElement), but that failed with the error: unexpected node type element. readelementstring method can only be called on elements

Checking an XElement for Existence of One of Several Possible XElements

喜欢而已 提交于 2019-12-06 15:44:59
Is there a way to determine if an XElement contains one of any specified elements? For example, I have XElements that I'll want to check: Dim xe1 = <color><blue/></color> Dim xe2 = <color><red/></color> Dim xe3 = <color><powderBlue/></color> Dim xe4 = <color><aqua/></color> Dim xe5 = <color><green/></color> I'd like to be able to query any of the xelements to see if they containt the elements <red/> , <green/> or <blue/> below them and return true if so, false if not. I was hoping that it would be simplier, but the best I could come up with was: Dim primaryColor = From e In xe1.Elements Where

Partially deserialize XML to Object

回眸只為那壹抹淺笑 提交于 2019-12-05 23:30:40
I have some XML that I deserialize into a business object. I am using XmlSerializer.Deserialize to do so. However, I want one of the XmlElement contained in the XML to stay an XElement. It cannot be done directly (using an XmlElementAttribute) since XElement is not Serializable. I also tried to serialize that element to a string (in a two steps attempt to get an XElement), but that failed with the error: unexpected node type element. readelementstring method can only be called on elements with simple or empty content Any idea how that can be done? Here is an example of xml and the resulting

Returning an XElement from a web service

こ雲淡風輕ζ 提交于 2019-12-05 16:54:02
Is it possible to return an XElement from a webservice (in C#/asp.net)? Try a simple web service that returns an XElement: [WebMethod] public XElement DoItXElement() { XElement xe = new XElement("hello", new XElement("message", "Hello World") ); return xe; } This compiles fine but if you try and run it you get Cannot use wildcards at the top level of a schema. I found this post implying that this is a bug in .net. So... Can I return an XElement from a web service? If so, how? Thanks. There appears to be an issue with how an XElement is serialized, check here ... You can try outing the XElement

Converting XElement into XmlNode

ⅰ亾dé卋堺 提交于 2019-12-05 09:18:58
问题 I know there is no direct method of doing it but still.. Can we convert XElement element into XmlNode . Options like InnerText and InnerXml are XmlNode specific. so,if i want to use these options, what can be done to convert XElement into XmlNode and vice versa. 回答1: Here is converting from string to XElement to XmlNode and back to XElement. ToString() on XElement is similar to OuterXml on XmlNode. XElement xE = XElement.Parse("<Outer><Inner><Data /></Inner></Outer>"); XmlDocument xD = new

Innertext from XElement? [duplicate]

非 Y 不嫁゛ 提交于 2019-12-05 06:57:35
This question already has answers here : Best way to get InnerXml of an XElement? (15 answers) Closed 5 years ago . I'm having a hard time getting the correct value from the innertext of an XElement. First, here's the XML that I'm using. This is a copy of our production data that results from a process in our workflow. In other words, I can't change the XML, I can only parse it. The element whose innertext I'd like to get has data inside that looks like XML, but it isn't. It is straight text from the tool that produced the XML. The element is called <creatorshapeutildata : Here is the line of

XElement value in C#

别说谁变了你拦得住时间么 提交于 2019-12-04 19:34:51
问题 How to get a value of XElement without getting child elements? An example: <?xml version="1.0" ?> <someNode> someValue <child>1</child> <child>2</child> </someNode> If i use XElement.Value for <someNode> I get "somevalue<child>1</child><child>2<child>" string but I want to get only "somevalue" without "<child>1</child><child>2<child>" substring. 回答1: You can do it slightly more simply than using Descendants - the Nodes method only returns the direct child nodes: XElement element = XElement

XElement and List<T>

让人想犯罪 __ 提交于 2019-12-04 03:14:46
I have a class that has the following properties: public class Author { public string FirstName { get; set; } public string LastName { get; set; } } Next, I have a List of Authors like so: List<Author> authors = new List<Author> (); authors.add( new Author { FirstName = "Steven", LastName = "King" }); authors.add( new Author { FirstName = "Homer", LastName = "" }); Now, I am trying to use Linq to XML in order to generate the XML representing my Authors List. new XElement("Authors", new XElement("Author", from na in this.Authors select new XElement("First", na.First))) The block above does not