linq-to-xml

Best LINQ-to-XML query to select nodes based on properties of descendant nodes?

孤街浪徒 提交于 2020-02-27 08:53:59
问题 I have the following XML document: <?xml version="1.0" encoding="UTF-8"?> <FamilyTree> <Parent name="Ken"> <Child name="Lorna"> <Grandchild name="Andrew"/> <Grandchild name="Brian"/> </Child> <Child name="Mike"> <Grandchild name="Ann"/> <Grandchild name="Beth"/> </Child> </Parent> <Parent name="Norma"> <Child name="Owen"> <Grandchild name="Charles"/> </Child> <Child name="Peter"> <Grandchild name="Charlotte"/> </Child> </Parent> <Parent name="Quinn"> <Child name="Robert"> <Grandchild name=

Get the depth of an Item

情到浓时终转凉″ 提交于 2020-01-29 13:29:09
问题 I have xml like this: <A><B>test</B><B><B>test2</B></B><B><B><B>test2</B></B></B></A> How can I get the level of each of these items using linq to xml level of test=1 level of test2=2 level of test3=3 I have no idea how many nodes there will be or how many levels there will be. I can write this as a recursive function but I thought linq to xml might have something better to offer. 回答1: Assuming you've loaded your XML as an XDocument or XElement object, myXElement.AncestorsAndSelf().Count()

C# getting a specific attribute of a child of a Previous node in XML while testing the node for a missing element

烈酒焚心 提交于 2020-01-25 20:14:26
问题 I have a xliff and I am trying to get the value of the id attribute of the last x element of source in the previous trans-unit so long as it does not have a target child. So very specifically, I don't just need to return all elements, but get the parent node, test if the previous has a target element and if so return null , but if it does not have then return the value of the id attribute of the last child x of the source element. Update: There are many trans-units in the xliff, the below is

C# getting a specific attribute of a child of a Previous node in XML while testing the node for a missing element

淺唱寂寞╮ 提交于 2020-01-25 20:14:09
问题 I have a xliff and I am trying to get the value of the id attribute of the last x element of source in the previous trans-unit so long as it does not have a target child. So very specifically, I don't just need to return all elements, but get the parent node, test if the previous has a target element and if so return null , but if it does not have then return the value of the id attribute of the last child x of the source element. Update: There are many trans-units in the xliff, the below is

Edit and Delete data from XML file using ASP.net and VB.net (or C#)

我怕爱的太早我们不能终老 提交于 2020-01-24 05:50:06
问题 In my web application , i have an XML file called "answers.xml" it store the user entries in XML <?xml version=""1.0""?> <Answers> <AnswerSet> <Answer questionId=""MRN"">4444</Answer> <Answer questionId=""FName"">test</Answer> <Answer questionId=""LName"">patient</Answer> <Answer questionId=""AddressPt"">blah blah</Answer> <Answer questionId=""Governorate"">xxxx</Answer> <Answer questionId=""InitialCSF"">Negative</Answer> <Answer questionId=""Diagnosis""></Answer> <Answer questionId="

Can I use XDocument.Save and exclude the XML Declaration?

时光怂恿深爱的人放手 提交于 2020-01-23 12:57:33
问题 I'm loading a XML file into a XDocument, doing stuff to it, and calling Save(fileName) to rewrite the XML file. The original file has no XML declaration, is there anyway to avoid XDocument adding a XML declaration? 回答1: I think you'll have to use an XmlWriter. Or ToString. Just found this article which seems to agree. 来源: https://stackoverflow.com/questions/711954/can-i-use-xdocument-save-and-exclude-the-xml-declaration

LINQ to XML recursive query

安稳与你 提交于 2020-01-23 12:07:51
问题 I have an xml sitemap structured like a document tree, such that it looks like this: <Site> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <Folder name="FolderName"> <Security> <Role>Admin</role> </Security> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <Folder name="subFoler"> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <Folder> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx

LINQ to XML recursive query

和自甴很熟 提交于 2020-01-23 12:06:12
问题 I have an xml sitemap structured like a document tree, such that it looks like this: <Site> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <Folder name="FolderName"> <Security> <Role>Admin</role> </Security> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <Folder name="subFoler"> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <Folder> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx

LINQ to XML recursive query

风格不统一 提交于 2020-01-23 12:05:44
问题 I have an xml sitemap structured like a document tree, such that it looks like this: <Site> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <Folder name="FolderName"> <Security> <Role>Admin</role> </Security> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <Folder name="subFoler"> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file> <Folder> <File GUID="xxxxxxxx-xxxx-xxxx-xxxx

How to Include CData using LINQ to XML?

故事扮演 提交于 2020-01-20 05:44:05
问题 I want to record to a XML with below those codes in ASP.Net.However,I want to add <![[CDATA]]> in the fifth element. when I making it as shown below, it's creating ""&"bt;" instead of > character and ""&"lt;" instead of < character to XML. How to get rid of that problem? Code: XElement xml = new XElement("photo", new XElement("thumbnail", TextBox1.Text), new XElement("filename", TextBox2.Text), new XElement("baslik1", TextBox3.Text), new XElement("baslik2", TextBox4.Text), new XElement(