Get specific XML data using C#
<hashtable>
<entry>
<string>recsonpage</string>
>
<string>1</string>
</entry>
<entry>
<string>recsindb</string>
<string>1</string>
</entry>
<entry>
<string>1</string>
<hashtable>
<entry>
<string>orders.orderid</string>
<string>84344969</string>
</entry>
<entry>
<string>entity.customerid</string>
<string>19421067</string>
</entry>
<entry>
<string>entity.entityid</string>
<string>84344969</string>
</entry>
<entry>
<string>orders.autorenew</string>
<string>false</string>
</entry>
<entry>
<string>orders.endtime</string>
<string>1572414460</string>
</entry>
<entry>
<string>orders.resellerlock</string>
<string>false</string>
</entry>
<entry>
<string>orders.timestamp</string>
<string>2018-10-30 05:47:41.791511+00</string>
</entry>
<entry>
<string>orders.customerlock</string>
<string>true</string>
</entry>
<entry>
<string>entity.entitytypeid</string>
<string>87</string>
</entry>
<entry>
<string>entity.currentstatus</string>
<string>Active</string>
</entry>
<entry>
<string>entitytype.entitytypekey</string>
<string>dotco</string>
</entry>
<entry>
<string>orders.transferlock</string>
<string>true</string>
</entry>
<entry>
<string>orders.creationtime</string>
<string>1540878460</string>
</entry>
<entry>
<string>orders.privacyprotection</string>
<string>false</string>
</entry>
<entry>
<string>entitytype.entitytypename</string>
<string>.CO Domain Name</string>
</entry>
<entry>
<string>orders.creationdt</string>
<string>1540878459</string>
</entry>
<entry>
<string>entity.description</string>
<string>testlbapi.co</string>
</entry>
</hashtable>
</entry>
</hashtable>
Hi All. I need help on how to get the data of orders.orderid, entity.description of this XML Data. I used XElement but it gets the whole content of this XML Data. Any idea on this? Thank you in advance. Hi All. I need help on how to get the data of orders.orderid, entity.description of this XML Data. I used XElement but it gets the whole content of this XML Data. Any idea on this? Thank you in advance.
c# xml
add a comment |
<hashtable>
<entry>
<string>recsonpage</string>
>
<string>1</string>
</entry>
<entry>
<string>recsindb</string>
<string>1</string>
</entry>
<entry>
<string>1</string>
<hashtable>
<entry>
<string>orders.orderid</string>
<string>84344969</string>
</entry>
<entry>
<string>entity.customerid</string>
<string>19421067</string>
</entry>
<entry>
<string>entity.entityid</string>
<string>84344969</string>
</entry>
<entry>
<string>orders.autorenew</string>
<string>false</string>
</entry>
<entry>
<string>orders.endtime</string>
<string>1572414460</string>
</entry>
<entry>
<string>orders.resellerlock</string>
<string>false</string>
</entry>
<entry>
<string>orders.timestamp</string>
<string>2018-10-30 05:47:41.791511+00</string>
</entry>
<entry>
<string>orders.customerlock</string>
<string>true</string>
</entry>
<entry>
<string>entity.entitytypeid</string>
<string>87</string>
</entry>
<entry>
<string>entity.currentstatus</string>
<string>Active</string>
</entry>
<entry>
<string>entitytype.entitytypekey</string>
<string>dotco</string>
</entry>
<entry>
<string>orders.transferlock</string>
<string>true</string>
</entry>
<entry>
<string>orders.creationtime</string>
<string>1540878460</string>
</entry>
<entry>
<string>orders.privacyprotection</string>
<string>false</string>
</entry>
<entry>
<string>entitytype.entitytypename</string>
<string>.CO Domain Name</string>
</entry>
<entry>
<string>orders.creationdt</string>
<string>1540878459</string>
</entry>
<entry>
<string>entity.description</string>
<string>testlbapi.co</string>
</entry>
</hashtable>
</entry>
</hashtable>
Hi All. I need help on how to get the data of orders.orderid, entity.description of this XML Data. I used XElement but it gets the whole content of this XML Data. Any idea on this? Thank you in advance. Hi All. I need help on how to get the data of orders.orderid, entity.description of this XML Data. I used XElement but it gets the whole content of this XML Data. Any idea on this? Thank you in advance.
c# xml
Hi @IrisLorrenHequibal, please, do not repeat your message just to get rid of SO warnings, instead try to provide more information, so that your chances of getting help will be higher!
– toti08
Nov 20 '18 at 7:01
You already asked the same thing about JSON. This XML schema is actually harder to query than a JSON file. What are you trying to do? Why not use proper elements and attributes instead of dumping a hashtable into unordered XML? A useful XML schema would be<entity><orders/><entityType/></entity>
.
– Panagiotis Kanavos
Nov 20 '18 at 7:43
This sounds like an XY Problem. You have a problem with X and assume the solution is Y. When that doesn't work, you ask about Y, not X. In this case you assume that X will be solved by changing how you serialize unordered data. What is the actual problem you want to solve? Where does this data come from? A database? Another system? Why not create proper objects to model it and serialize it to XML or JSON?
– Panagiotis Kanavos
Nov 20 '18 at 7:47
it's coming from an API URL.
– Iris Lorren Hequibal
Nov 20 '18 at 8:28
Pretty printed the xml.
– LosManos
Nov 20 '18 at 9:35
add a comment |
<hashtable>
<entry>
<string>recsonpage</string>
>
<string>1</string>
</entry>
<entry>
<string>recsindb</string>
<string>1</string>
</entry>
<entry>
<string>1</string>
<hashtable>
<entry>
<string>orders.orderid</string>
<string>84344969</string>
</entry>
<entry>
<string>entity.customerid</string>
<string>19421067</string>
</entry>
<entry>
<string>entity.entityid</string>
<string>84344969</string>
</entry>
<entry>
<string>orders.autorenew</string>
<string>false</string>
</entry>
<entry>
<string>orders.endtime</string>
<string>1572414460</string>
</entry>
<entry>
<string>orders.resellerlock</string>
<string>false</string>
</entry>
<entry>
<string>orders.timestamp</string>
<string>2018-10-30 05:47:41.791511+00</string>
</entry>
<entry>
<string>orders.customerlock</string>
<string>true</string>
</entry>
<entry>
<string>entity.entitytypeid</string>
<string>87</string>
</entry>
<entry>
<string>entity.currentstatus</string>
<string>Active</string>
</entry>
<entry>
<string>entitytype.entitytypekey</string>
<string>dotco</string>
</entry>
<entry>
<string>orders.transferlock</string>
<string>true</string>
</entry>
<entry>
<string>orders.creationtime</string>
<string>1540878460</string>
</entry>
<entry>
<string>orders.privacyprotection</string>
<string>false</string>
</entry>
<entry>
<string>entitytype.entitytypename</string>
<string>.CO Domain Name</string>
</entry>
<entry>
<string>orders.creationdt</string>
<string>1540878459</string>
</entry>
<entry>
<string>entity.description</string>
<string>testlbapi.co</string>
</entry>
</hashtable>
</entry>
</hashtable>
Hi All. I need help on how to get the data of orders.orderid, entity.description of this XML Data. I used XElement but it gets the whole content of this XML Data. Any idea on this? Thank you in advance. Hi All. I need help on how to get the data of orders.orderid, entity.description of this XML Data. I used XElement but it gets the whole content of this XML Data. Any idea on this? Thank you in advance.
c# xml
<hashtable>
<entry>
<string>recsonpage</string>
>
<string>1</string>
</entry>
<entry>
<string>recsindb</string>
<string>1</string>
</entry>
<entry>
<string>1</string>
<hashtable>
<entry>
<string>orders.orderid</string>
<string>84344969</string>
</entry>
<entry>
<string>entity.customerid</string>
<string>19421067</string>
</entry>
<entry>
<string>entity.entityid</string>
<string>84344969</string>
</entry>
<entry>
<string>orders.autorenew</string>
<string>false</string>
</entry>
<entry>
<string>orders.endtime</string>
<string>1572414460</string>
</entry>
<entry>
<string>orders.resellerlock</string>
<string>false</string>
</entry>
<entry>
<string>orders.timestamp</string>
<string>2018-10-30 05:47:41.791511+00</string>
</entry>
<entry>
<string>orders.customerlock</string>
<string>true</string>
</entry>
<entry>
<string>entity.entitytypeid</string>
<string>87</string>
</entry>
<entry>
<string>entity.currentstatus</string>
<string>Active</string>
</entry>
<entry>
<string>entitytype.entitytypekey</string>
<string>dotco</string>
</entry>
<entry>
<string>orders.transferlock</string>
<string>true</string>
</entry>
<entry>
<string>orders.creationtime</string>
<string>1540878460</string>
</entry>
<entry>
<string>orders.privacyprotection</string>
<string>false</string>
</entry>
<entry>
<string>entitytype.entitytypename</string>
<string>.CO Domain Name</string>
</entry>
<entry>
<string>orders.creationdt</string>
<string>1540878459</string>
</entry>
<entry>
<string>entity.description</string>
<string>testlbapi.co</string>
</entry>
</hashtable>
</entry>
</hashtable>
Hi All. I need help on how to get the data of orders.orderid, entity.description of this XML Data. I used XElement but it gets the whole content of this XML Data. Any idea on this? Thank you in advance. Hi All. I need help on how to get the data of orders.orderid, entity.description of this XML Data. I used XElement but it gets the whole content of this XML Data. Any idea on this? Thank you in advance.
c# xml
c# xml
edited Nov 23 '18 at 10:21
er-sho
6,0672617
6,0672617
asked Nov 20 '18 at 6:34


Iris Lorren HequibalIris Lorren Hequibal
12
12
Hi @IrisLorrenHequibal, please, do not repeat your message just to get rid of SO warnings, instead try to provide more information, so that your chances of getting help will be higher!
– toti08
Nov 20 '18 at 7:01
You already asked the same thing about JSON. This XML schema is actually harder to query than a JSON file. What are you trying to do? Why not use proper elements and attributes instead of dumping a hashtable into unordered XML? A useful XML schema would be<entity><orders/><entityType/></entity>
.
– Panagiotis Kanavos
Nov 20 '18 at 7:43
This sounds like an XY Problem. You have a problem with X and assume the solution is Y. When that doesn't work, you ask about Y, not X. In this case you assume that X will be solved by changing how you serialize unordered data. What is the actual problem you want to solve? Where does this data come from? A database? Another system? Why not create proper objects to model it and serialize it to XML or JSON?
– Panagiotis Kanavos
Nov 20 '18 at 7:47
it's coming from an API URL.
– Iris Lorren Hequibal
Nov 20 '18 at 8:28
Pretty printed the xml.
– LosManos
Nov 20 '18 at 9:35
add a comment |
Hi @IrisLorrenHequibal, please, do not repeat your message just to get rid of SO warnings, instead try to provide more information, so that your chances of getting help will be higher!
– toti08
Nov 20 '18 at 7:01
You already asked the same thing about JSON. This XML schema is actually harder to query than a JSON file. What are you trying to do? Why not use proper elements and attributes instead of dumping a hashtable into unordered XML? A useful XML schema would be<entity><orders/><entityType/></entity>
.
– Panagiotis Kanavos
Nov 20 '18 at 7:43
This sounds like an XY Problem. You have a problem with X and assume the solution is Y. When that doesn't work, you ask about Y, not X. In this case you assume that X will be solved by changing how you serialize unordered data. What is the actual problem you want to solve? Where does this data come from? A database? Another system? Why not create proper objects to model it and serialize it to XML or JSON?
– Panagiotis Kanavos
Nov 20 '18 at 7:47
it's coming from an API URL.
– Iris Lorren Hequibal
Nov 20 '18 at 8:28
Pretty printed the xml.
– LosManos
Nov 20 '18 at 9:35
Hi @IrisLorrenHequibal, please, do not repeat your message just to get rid of SO warnings, instead try to provide more information, so that your chances of getting help will be higher!
– toti08
Nov 20 '18 at 7:01
Hi @IrisLorrenHequibal, please, do not repeat your message just to get rid of SO warnings, instead try to provide more information, so that your chances of getting help will be higher!
– toti08
Nov 20 '18 at 7:01
You already asked the same thing about JSON. This XML schema is actually harder to query than a JSON file. What are you trying to do? Why not use proper elements and attributes instead of dumping a hashtable into unordered XML? A useful XML schema would be
<entity><orders/><entityType/></entity>
.– Panagiotis Kanavos
Nov 20 '18 at 7:43
You already asked the same thing about JSON. This XML schema is actually harder to query than a JSON file. What are you trying to do? Why not use proper elements and attributes instead of dumping a hashtable into unordered XML? A useful XML schema would be
<entity><orders/><entityType/></entity>
.– Panagiotis Kanavos
Nov 20 '18 at 7:43
This sounds like an XY Problem. You have a problem with X and assume the solution is Y. When that doesn't work, you ask about Y, not X. In this case you assume that X will be solved by changing how you serialize unordered data. What is the actual problem you want to solve? Where does this data come from? A database? Another system? Why not create proper objects to model it and serialize it to XML or JSON?
– Panagiotis Kanavos
Nov 20 '18 at 7:47
This sounds like an XY Problem. You have a problem with X and assume the solution is Y. When that doesn't work, you ask about Y, not X. In this case you assume that X will be solved by changing how you serialize unordered data. What is the actual problem you want to solve? Where does this data come from? A database? Another system? Why not create proper objects to model it and serialize it to XML or JSON?
– Panagiotis Kanavos
Nov 20 '18 at 7:47
it's coming from an API URL.
– Iris Lorren Hequibal
Nov 20 '18 at 8:28
it's coming from an API URL.
– Iris Lorren Hequibal
Nov 20 '18 at 8:28
Pretty printed the xml.
– LosManos
Nov 20 '18 at 9:35
Pretty printed the xml.
– LosManos
Nov 20 '18 at 9:35
add a comment |
2 Answers
2
active
oldest
votes
If you already have an XElement root
representing the whole tree, you can iterate over the entries:
foreach (var entry in root.Elements("entry")
{
...
}
Each entry seems to have two sub-elements: one <string>key</string>
and another <type>...</type>
(where type can be string or hashtable).
To get the key string, you could use
var key = entry.Elements().First().Value;
For the value, you have to discriminate according to the element name:
var valueElement = entry.Elements().ElementAt(1);
if (valueElement.Name == "string")
{
var value = valueElement.Value;
}
else if (valueElement == "hashtable")
{
// here, you have to recurse with valueElement instead of root
}
EDIT: Shorter way to get only value of orders.orderid:
var orderIdElement = root.Descendants("entry").FirstOrDefault(_ => _.Elements().First().Value == "orders.orderid");
var orderId = orderIdElement .Elements().ElementAt(1).Value;
but my question is how will I only get the content of orders.orderid and entity.description on the XML data I provided?
– Iris Lorren Hequibal
Nov 20 '18 at 7:33
Added this special case to my answer.
– Klaus Gütter
Nov 20 '18 at 7:38
add a comment |
You can use below function to fetch specific sibling element text.
public string ExtractNodeValue(XDocument doc, string search)
{
var result = doc.Descendants("entry")
.Where(x => x.Elements().ElementAt(0).Value == search)
.Select(i => (string)i.Elements().ElementAt(1).Value)
.FirstOrDefault();
return result;
}
And you can use above function like
XDocument doc = XDocument.Load(@"Url to xml file");
string orderId = ExtractNodeValue(doc, "orders.orderid");
string description = ExtractNodeValue(doc, "entity.description");
string output = "orders.orderid: " + orderId + "nentity.description: " + description;
Output from Debugger:
Hi, I tried your code but ExtractNodeValue can't be recognized in string orderID and string description.
– Iris Lorren Hequibal
Nov 20 '18 at 10:38
@IrisLorrenHequibal, this is an exception? then please paste exception message so I'll try to resolve it
– er-sho
Nov 20 '18 at 10:41
it's working now. thank you :)
– Iris Lorren Hequibal
Nov 20 '18 at 10:43
@IrisLorrenHequibal, If answer was help you then mark the tick on left side of answer to make it green :)
– er-sho
Nov 20 '18 at 10:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387495%2fget-specific-xml-data-using-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you already have an XElement root
representing the whole tree, you can iterate over the entries:
foreach (var entry in root.Elements("entry")
{
...
}
Each entry seems to have two sub-elements: one <string>key</string>
and another <type>...</type>
(where type can be string or hashtable).
To get the key string, you could use
var key = entry.Elements().First().Value;
For the value, you have to discriminate according to the element name:
var valueElement = entry.Elements().ElementAt(1);
if (valueElement.Name == "string")
{
var value = valueElement.Value;
}
else if (valueElement == "hashtable")
{
// here, you have to recurse with valueElement instead of root
}
EDIT: Shorter way to get only value of orders.orderid:
var orderIdElement = root.Descendants("entry").FirstOrDefault(_ => _.Elements().First().Value == "orders.orderid");
var orderId = orderIdElement .Elements().ElementAt(1).Value;
but my question is how will I only get the content of orders.orderid and entity.description on the XML data I provided?
– Iris Lorren Hequibal
Nov 20 '18 at 7:33
Added this special case to my answer.
– Klaus Gütter
Nov 20 '18 at 7:38
add a comment |
If you already have an XElement root
representing the whole tree, you can iterate over the entries:
foreach (var entry in root.Elements("entry")
{
...
}
Each entry seems to have two sub-elements: one <string>key</string>
and another <type>...</type>
(where type can be string or hashtable).
To get the key string, you could use
var key = entry.Elements().First().Value;
For the value, you have to discriminate according to the element name:
var valueElement = entry.Elements().ElementAt(1);
if (valueElement.Name == "string")
{
var value = valueElement.Value;
}
else if (valueElement == "hashtable")
{
// here, you have to recurse with valueElement instead of root
}
EDIT: Shorter way to get only value of orders.orderid:
var orderIdElement = root.Descendants("entry").FirstOrDefault(_ => _.Elements().First().Value == "orders.orderid");
var orderId = orderIdElement .Elements().ElementAt(1).Value;
but my question is how will I only get the content of orders.orderid and entity.description on the XML data I provided?
– Iris Lorren Hequibal
Nov 20 '18 at 7:33
Added this special case to my answer.
– Klaus Gütter
Nov 20 '18 at 7:38
add a comment |
If you already have an XElement root
representing the whole tree, you can iterate over the entries:
foreach (var entry in root.Elements("entry")
{
...
}
Each entry seems to have two sub-elements: one <string>key</string>
and another <type>...</type>
(where type can be string or hashtable).
To get the key string, you could use
var key = entry.Elements().First().Value;
For the value, you have to discriminate according to the element name:
var valueElement = entry.Elements().ElementAt(1);
if (valueElement.Name == "string")
{
var value = valueElement.Value;
}
else if (valueElement == "hashtable")
{
// here, you have to recurse with valueElement instead of root
}
EDIT: Shorter way to get only value of orders.orderid:
var orderIdElement = root.Descendants("entry").FirstOrDefault(_ => _.Elements().First().Value == "orders.orderid");
var orderId = orderIdElement .Elements().ElementAt(1).Value;
If you already have an XElement root
representing the whole tree, you can iterate over the entries:
foreach (var entry in root.Elements("entry")
{
...
}
Each entry seems to have two sub-elements: one <string>key</string>
and another <type>...</type>
(where type can be string or hashtable).
To get the key string, you could use
var key = entry.Elements().First().Value;
For the value, you have to discriminate according to the element name:
var valueElement = entry.Elements().ElementAt(1);
if (valueElement.Name == "string")
{
var value = valueElement.Value;
}
else if (valueElement == "hashtable")
{
// here, you have to recurse with valueElement instead of root
}
EDIT: Shorter way to get only value of orders.orderid:
var orderIdElement = root.Descendants("entry").FirstOrDefault(_ => _.Elements().First().Value == "orders.orderid");
var orderId = orderIdElement .Elements().ElementAt(1).Value;
edited Nov 20 '18 at 7:37
answered Nov 20 '18 at 6:49


Klaus GütterKlaus Gütter
2,51821321
2,51821321
but my question is how will I only get the content of orders.orderid and entity.description on the XML data I provided?
– Iris Lorren Hequibal
Nov 20 '18 at 7:33
Added this special case to my answer.
– Klaus Gütter
Nov 20 '18 at 7:38
add a comment |
but my question is how will I only get the content of orders.orderid and entity.description on the XML data I provided?
– Iris Lorren Hequibal
Nov 20 '18 at 7:33
Added this special case to my answer.
– Klaus Gütter
Nov 20 '18 at 7:38
but my question is how will I only get the content of orders.orderid and entity.description on the XML data I provided?
– Iris Lorren Hequibal
Nov 20 '18 at 7:33
but my question is how will I only get the content of orders.orderid and entity.description on the XML data I provided?
– Iris Lorren Hequibal
Nov 20 '18 at 7:33
Added this special case to my answer.
– Klaus Gütter
Nov 20 '18 at 7:38
Added this special case to my answer.
– Klaus Gütter
Nov 20 '18 at 7:38
add a comment |
You can use below function to fetch specific sibling element text.
public string ExtractNodeValue(XDocument doc, string search)
{
var result = doc.Descendants("entry")
.Where(x => x.Elements().ElementAt(0).Value == search)
.Select(i => (string)i.Elements().ElementAt(1).Value)
.FirstOrDefault();
return result;
}
And you can use above function like
XDocument doc = XDocument.Load(@"Url to xml file");
string orderId = ExtractNodeValue(doc, "orders.orderid");
string description = ExtractNodeValue(doc, "entity.description");
string output = "orders.orderid: " + orderId + "nentity.description: " + description;
Output from Debugger:
Hi, I tried your code but ExtractNodeValue can't be recognized in string orderID and string description.
– Iris Lorren Hequibal
Nov 20 '18 at 10:38
@IrisLorrenHequibal, this is an exception? then please paste exception message so I'll try to resolve it
– er-sho
Nov 20 '18 at 10:41
it's working now. thank you :)
– Iris Lorren Hequibal
Nov 20 '18 at 10:43
@IrisLorrenHequibal, If answer was help you then mark the tick on left side of answer to make it green :)
– er-sho
Nov 20 '18 at 10:45
add a comment |
You can use below function to fetch specific sibling element text.
public string ExtractNodeValue(XDocument doc, string search)
{
var result = doc.Descendants("entry")
.Where(x => x.Elements().ElementAt(0).Value == search)
.Select(i => (string)i.Elements().ElementAt(1).Value)
.FirstOrDefault();
return result;
}
And you can use above function like
XDocument doc = XDocument.Load(@"Url to xml file");
string orderId = ExtractNodeValue(doc, "orders.orderid");
string description = ExtractNodeValue(doc, "entity.description");
string output = "orders.orderid: " + orderId + "nentity.description: " + description;
Output from Debugger:
Hi, I tried your code but ExtractNodeValue can't be recognized in string orderID and string description.
– Iris Lorren Hequibal
Nov 20 '18 at 10:38
@IrisLorrenHequibal, this is an exception? then please paste exception message so I'll try to resolve it
– er-sho
Nov 20 '18 at 10:41
it's working now. thank you :)
– Iris Lorren Hequibal
Nov 20 '18 at 10:43
@IrisLorrenHequibal, If answer was help you then mark the tick on left side of answer to make it green :)
– er-sho
Nov 20 '18 at 10:45
add a comment |
You can use below function to fetch specific sibling element text.
public string ExtractNodeValue(XDocument doc, string search)
{
var result = doc.Descendants("entry")
.Where(x => x.Elements().ElementAt(0).Value == search)
.Select(i => (string)i.Elements().ElementAt(1).Value)
.FirstOrDefault();
return result;
}
And you can use above function like
XDocument doc = XDocument.Load(@"Url to xml file");
string orderId = ExtractNodeValue(doc, "orders.orderid");
string description = ExtractNodeValue(doc, "entity.description");
string output = "orders.orderid: " + orderId + "nentity.description: " + description;
Output from Debugger:
You can use below function to fetch specific sibling element text.
public string ExtractNodeValue(XDocument doc, string search)
{
var result = doc.Descendants("entry")
.Where(x => x.Elements().ElementAt(0).Value == search)
.Select(i => (string)i.Elements().ElementAt(1).Value)
.FirstOrDefault();
return result;
}
And you can use above function like
XDocument doc = XDocument.Load(@"Url to xml file");
string orderId = ExtractNodeValue(doc, "orders.orderid");
string description = ExtractNodeValue(doc, "entity.description");
string output = "orders.orderid: " + orderId + "nentity.description: " + description;
Output from Debugger:
answered Nov 20 '18 at 10:00
er-shoer-sho
6,0672617
6,0672617
Hi, I tried your code but ExtractNodeValue can't be recognized in string orderID and string description.
– Iris Lorren Hequibal
Nov 20 '18 at 10:38
@IrisLorrenHequibal, this is an exception? then please paste exception message so I'll try to resolve it
– er-sho
Nov 20 '18 at 10:41
it's working now. thank you :)
– Iris Lorren Hequibal
Nov 20 '18 at 10:43
@IrisLorrenHequibal, If answer was help you then mark the tick on left side of answer to make it green :)
– er-sho
Nov 20 '18 at 10:45
add a comment |
Hi, I tried your code but ExtractNodeValue can't be recognized in string orderID and string description.
– Iris Lorren Hequibal
Nov 20 '18 at 10:38
@IrisLorrenHequibal, this is an exception? then please paste exception message so I'll try to resolve it
– er-sho
Nov 20 '18 at 10:41
it's working now. thank you :)
– Iris Lorren Hequibal
Nov 20 '18 at 10:43
@IrisLorrenHequibal, If answer was help you then mark the tick on left side of answer to make it green :)
– er-sho
Nov 20 '18 at 10:45
Hi, I tried your code but ExtractNodeValue can't be recognized in string orderID and string description.
– Iris Lorren Hequibal
Nov 20 '18 at 10:38
Hi, I tried your code but ExtractNodeValue can't be recognized in string orderID and string description.
– Iris Lorren Hequibal
Nov 20 '18 at 10:38
@IrisLorrenHequibal, this is an exception? then please paste exception message so I'll try to resolve it
– er-sho
Nov 20 '18 at 10:41
@IrisLorrenHequibal, this is an exception? then please paste exception message so I'll try to resolve it
– er-sho
Nov 20 '18 at 10:41
it's working now. thank you :)
– Iris Lorren Hequibal
Nov 20 '18 at 10:43
it's working now. thank you :)
– Iris Lorren Hequibal
Nov 20 '18 at 10:43
@IrisLorrenHequibal, If answer was help you then mark the tick on left side of answer to make it green :)
– er-sho
Nov 20 '18 at 10:45
@IrisLorrenHequibal, If answer was help you then mark the tick on left side of answer to make it green :)
– er-sho
Nov 20 '18 at 10:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387495%2fget-specific-xml-data-using-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi @IrisLorrenHequibal, please, do not repeat your message just to get rid of SO warnings, instead try to provide more information, so that your chances of getting help will be higher!
– toti08
Nov 20 '18 at 7:01
You already asked the same thing about JSON. This XML schema is actually harder to query than a JSON file. What are you trying to do? Why not use proper elements and attributes instead of dumping a hashtable into unordered XML? A useful XML schema would be
<entity><orders/><entityType/></entity>
.– Panagiotis Kanavos
Nov 20 '18 at 7:43
This sounds like an XY Problem. You have a problem with X and assume the solution is Y. When that doesn't work, you ask about Y, not X. In this case you assume that X will be solved by changing how you serialize unordered data. What is the actual problem you want to solve? Where does this data come from? A database? Another system? Why not create proper objects to model it and serialize it to XML or JSON?
– Panagiotis Kanavos
Nov 20 '18 at 7:47
it's coming from an API URL.
– Iris Lorren Hequibal
Nov 20 '18 at 8:28
Pretty printed the xml.
– LosManos
Nov 20 '18 at 9:35