linq-to-xml

Jaxb equivalent in C#

大憨熊 提交于 2019-12-17 15:58:07
问题 Using JAXB in Java it is easy to generate from a xml schema file a set of Java classes that xml conforming to that schema can be deserialized to. Is there some C# equivalent of JAXB? I know that Linq can serialize and deserialize classes to/from xml files. But how can I generate C# classes from xml schema file and then use this classes with linq? 回答1: If you're using Visual Studio, try the XML Schema Definition Tool. It takes your schema definitions and produces C# classes -- or it can go the

Remove empty/blanks elements in collection of XML nodes

我的梦境 提交于 2019-12-17 12:02:29
问题 I have an XML document like this: <magento_api> <data_item> <code>400</code> <message>Attribute weight is not applicable for product type Configurable Product</message> </data_item> <data_item> <code>400</code> <message>Resource data pre-validation error.</message> </data_item> <data_item> <code>1</code> <message></message> </data_item> <data_item> <code></code> <message>No code was given</message> </data_item> </magento_api> I'm trying to iterate each node and do the following: Throw out any

How to use XPath with XElement or LINQ?

我的未来我决定 提交于 2019-12-17 08:34:27
问题 Consider the following XML: <response> <status_code>200</status_code> <status_txt>OK</status_txt> <data> <url>http://bit.ly/b47LVi</url> <hash>b47LVi</hash> <global_hash>9EJa3m</global_hash> <long_url>http://www.tumblr.com/docs/en/api#api_write</long_url> <new_hash>0</new_hash> </data> </response> I'm looking for a really short way to get just the value of the <hash> element. I tried: var hash = xml.Element("hash").Value; But that's not working. Is it possible to provide an XPath query to an

XDocument.Validate is always successful

坚强是说给别人听的谎言 提交于 2019-12-17 05:13:07
问题 I have a schema file which does not define any target namespaces, i.e. its definition looks like this: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <!--Elements, attributes, etc. --> </xs:schema> The corresponding XML looks like this: <Documents p1:CRC="0" p1:Date="1900-01-01T01:01:01+01:00" p1:Name="Test" p1:Status="new" xmlns:p1="http://www.tempuri.org/pdms.xsd" xmlns="http://www.tempuri.org/pdms.xsd"> <p1:Document p1:Date="2010-12-23T07:59:45" p1

How to load an XHTML file into an XElement using a custom XmlUrlResolver?

人盡茶涼 提交于 2019-12-14 04:25:51
问题 I am trying to get an XHTML file loaded into an LINQ XElement. However, I am running into problems with the resolver. The problem has to do with the following definition: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> I have a custom XmlUrlResolver with an overridden GetEntity which converts links such as http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd to a local resource stream. This works fine for almost the

Looping through an XML document and assigning data in C# variables

淺唱寂寞╮ 提交于 2019-12-14 03:09:49
问题 I have an XML file that has several tags like this: <sitecollection name=""> <site name=""> <maingroup name=""> <group name=""> </group> </maingroup> </site> <sitecollection> The idea is to loop through all the sitecollection and it's child elements in the XML document, and save the info in variables. The problem I'm having is saving the child elements, with their attributes. So far I have the following code: class xmlreader { public static void Main() { XDocument xdoc = XDocument.Load(

Save/load 2 XDocuments to/from one stream

狂风中的少年 提交于 2019-12-14 03:08:58
问题 I've got 2 XDocuments. One is some meta data, the other is a lot of data. On the Xbox (XNA), I'd like to be able to save both to a file stream, meta data XDoc first, then the actual data XDoc. I'd then like to be able to access just the meta data XDoc (ignoring the rest of the file stream), and also to be able to access the meta data XDoc and the data XDoc. Currently i'm saving/loading as follows: public void Serialise(Stream SaveStream, object Obj) { XDocument XDoc = new XDocument(new

Linq to XML simple get attribute from node statement

≡放荡痞女 提交于 2019-12-14 02:18:34
问题 Here's the code snippet: XDocument themes = XDocument.Load(HttpContext.Current.Server.MapPath("~/Models/Themes.xml")); string result = ""; var childType = from t in themes.Descendants() where t.Attribute("name").Value.Equals(theme) select new { value = t.Attribute("type").Value }; foreach (var t in childType) { result += t.value; } return result; and here's the XML: <?xml version="1.0" encoding="utf-8" ?> <themes> <theme name="Agile"> <root type="Project"> <node type="Iteration" > <node type=

How to delete nodes and subnodes using XDocument

一笑奈何 提交于 2019-12-13 23:39:25
问题 I have xml as under <databaseserver> <server name="server1" serverid="1"> <databases> <database dbid="1" dbname="db1" /> <database dbid="2" dbname="db2" /> </databases> </server> <server name="server2" serverid="2"> <databases> <database dbid="1" dbname="db1" /> <database dbid="2" dbname="db2" /> </databases> </server> <server name="server3" serverid="3"> <databases> <database dbid="1" dbname="db1" /> <database dbid="2" dbname="db2" /> </databases> </server> </databaseserver> I want to delete

Add to a collection where is XElement(s)

…衆ロ難τιáo~ 提交于 2019-12-13 18:25:52
问题 I'm dealing with an XML file that has a very nested structure: <?xml version="1.0" encoding="utf-8"?> <Root> <Stuff> <Item Class="Consumable"> <CalledAs>ResourceAlpha</CalledAs> <ID>ResourceAlpha25367</ID> </Item> <Item Class="Consumable"> <CalledAs>ResourceAlpha</CalledAs> <ID>ResourceAlpha25368</ID> </Item> <Item Class="Consumable"> <CalledAs>ResourceAlpha</CalledAs> <ID>ResourceAlpha25371</ID> </Item> <Item Class="Consumable"> <CalledAs>ResourceAlpha</CalledAs> <ID>ResourceAlpha25372</ID>