Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've got this piece of XML, and I just need the value of an specific key. Like the sting input is "c4ca4238a0b923820dcc509a6f75849b" and it just needs to read: "Lorem Ispum".
<data>
<key id="c4ca4238a0b923820dcc509a6f75849b" alt-id="1">
<value>Lorem Ispum</value>
</key>
<key id="c81e728d9d4c2f636f067f89cc14862c" alt-id="2">
<value>Dolor Sit Amet</value>
</key>
</data>
This might do the trick for you
XDocument xdc = XDocument.Load("YourXMLFile");
var SomeValue = xdc.Descendants("key")
.Where(x => x.Attribute("id").Value == "c4ca4238a0b923820dcc509a6f75849b")
.Descendants("value")
.FirstOrDefault()
.Value;
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need a help to parse the below XML, What I wanted to do is the count of the entry id, below XML has 5 for entry id's. So What code should I write on C# to get the count from this XML? Thanks in advance
<entry_list version="1.0">
<entry id="cipher[1]">...</entry>
<entry id="cipher[2]">...</entry>
<entry id="cypher">...</entry>
<entry id="substitution cipher">...</entry>
<entry id="transposition cipher">...</entry>
</entry_list>
Try this:
var xdoc = XDocument.Load(#"FileToLoad.xml");
var count = xdoc.Descendants("entry").Count(); // 5
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to know if it is possible to extract and element from a XML in the following fashion. I have tried multiple options such as LinqToXMl and XPath.
<Paper>
<HeaderText>
StackOverFlow
</HeaderText>
</Paper>
For the above XML, if my input is "HeaderText" (the element to be retrieved).
How can I do that without accessing the root element?
Thank You!
XPath has a double-slash which looks anywhere:
//HeaderText/text()
That will return Stack Overflow.
I used the following code to get this working.
XmlDocument document = new XmlDocument();
document.LoadXml(requestXmlString);
XmlNodeList nodes = document.DocumentElement.SelectNodes("//HeaderText");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am using XML Auto in my SQL Server to query data out. I want to know how best to parse through the data that it provides.
My end goal is to provide a list of items with their values, but I don't know how many columns or what the names of the columns will be until at least the data is pulled form SQL at runtime.
Here is a sample XML
<animals>
<animal name="Pig">
<meat>
<name>Prosciutto</name>
</meat>
<meat>
<name>Speck</name>
</meat>
</animal>
<animal name="Cow">
<meat>
<name>Clod</name>
</meat>
<meat>
<name>Brisket</name>
</meat>
<meat>
<name>Tri-tip</name>
</meat>
</animal>
<animal name="Chicken">
<meat>
<name>Drumstick</name>
</meat>
</animal>
</animals>
How can I show this in a list, and perform Linq2SQL (or Linq2XML)?
To select a list
var q = from x in doc.Descendants()
select x.Value;
to select specific nodes, e.g. animals, do doc.Descendants("animal")
to select specific value, e.g. name of an animal do select x.Attribute("name").Value
Demo: https://dotnetfiddle.net/TdCWjE
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a document (xml) where each line has a format like this
<Field ID="{475c2610-c157-4b91-9e2d-6855031b3538}" Name="FullName" DisplayName="Full name" Type="Text" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="FullName" ColName="nvarchar6" Required="FALSE" Hidden="TRUE" ReadOnly="FALSE" PITarget="" PrimaryPITarget="" PIAttribute="" PrimaryPIAttribute="" Aggregation="" Node="" />
The properties ColName="<>" and ID="{<>}" are supposed to be unique for every line that exists in the document.
How can I loop through each line and and see if the values inside ColName and ID appears more than once(preferrably though C#)?
Treating it like code golf... to get duplicate elements:
var duplicateElements = XDocument.Load(pathToDocument).Root.Elements()
.GroupBy(el => String.Format("{0}|{1}", el.Attribute("ID").Value, el.Attribute("ColName").Value)))
.Where((val, e) => val.Count() > 1);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to check if xml tag exist in C #?
My Xml
<name>
<firstname>John</firstname>
<lastname>cena</lastname>
<job>Administrator</job>
<location>sunnyvale</location>
<age>19</age>
</name>
<name>
<firstname>mark</firstname>
<job>Agent</job>
<location>Bangalore</location>
<age>22</age>
</name>
lastname tag doesn't exist for mark how to check in C#?
string xml = #"
<name>
<firstname>mark</firstname>
<job>Agent</job>
<location>Bangalore</location>
<age>22</age>
</name>";
var doc = XDocument.Parse(xml);
bool isLastNameExists = doc.Descendants("name").Elements("lastname").Any();