0

I have an xml string and have different records within and i want to extract the id within each record. Here is a sample of the xml:

<UploadsInformation >
    <Record>
        <TaskGUID>48A583CA-A532-419A-9CDB-292764CEC541</TaskGUID>
    </Record>
    <Record>
        <TaskGUID>ED6BA682-2BB2-4ADF-8355-9C605E16E088</TaskGUID>
    </Record>
    <Record>
        <TaskGUID>D20D7042-FC5B-4CF7-9496-D2D9DB68CF52</TaskGUID>
    </Record>
    <Record>
        <TaskGUID>F5DB10C5-D517-4CDA-8AAA-4E3F50B5FF3C</TaskGUID>
    </Record>
</UploadsInformation>

This is what i have as a string to extract the information that i need but not sure if it correct or not because when i debug the string seems to be the xml file and not just the specified guid.

string data = new XDocument(new XElement("Record", 
    uploads.Select(guid => new XElement("TaskGUID", guid.ToString()))))
    .ToString();

uploads is: List<Guid?> uploads

10
  • You would like to have a List<Guid?> as the final result? Commented Jul 1, 2013 at 9:31
  • @Bazzz yes since i will most likely have a list of taskGuids to loop through Commented Jul 1, 2013 at 9:32
  • You're creating a new XML document with your current query, that's why you're seeing XML. What do you want to be the end result of your query? The values in "TaskGUID" from the source XML? Commented Jul 1, 2013 at 9:32
  • @Tim yes just the TaskGUID values because i will be using those as a reference in a later step. Commented Jul 1, 2013 at 9:33
  • @loop852 Having List<Guid?> as the final result doesn't make a lot of sense, what purpose does a particular Guid? object that is null serve according to you? Perhaps a List<Guid> is a better approach? Commented Jul 1, 2013 at 9:34

3 Answers 3

2

If I understand your question correctly, you want to extract the Guids from the source XML, which you indicate is a string.

You can create an XDocument from a string with the following command:

XDocument doc = XDocument.Parse(xmlString);

XNamespace ns = "http://schemas.acatar.com/2013/03/Malt.Models";

List<string> uploads = doc.Descendants(ns + "TaskGUID")
                       .Select(x => x.Value).ToList();

string uploadString = String.Join(",", uploads);

I used XNamespace because there is a namespace (two, actually) defined in the XML, and unless you prefix the correct one to the element name you won't get any results.

You might be able to combine the last two steps into one line, but I'm not 100% sure.

The above code was tested with your example, and produces the following value for uploadString:

48A583CA-A532-419A-9CDB-292764CEC541,ED6BA682-2BB2-4ADF-8355-9C605E16E088,D20D7042-FC5B-4CF7-9496-D2D9DB68CF52,F5DB10C5-D517-4CDA-8AAA-4E3F50B5FF3C

However, if you're going to loop through the result and pass each one in singularly to a stored procedure, I'd skip the String.Join and just loop through the List:

foreach (string id in uploads)
{

    // Do your stored procedure call for each Guid.
}

Added in Response to Comment

In the situation in your comment, if you have a List that you want to get the values for, you'd do essentially the same, but you'll need to check for nulls and (probably) convert the Guid to a string before passing it into the stored proc:

foreach (Guid? g in uploads)
{

    if (g != null)
    {

        string newGuid = g.ToString();

        // do your data access stuff here
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Great - also i just noticed in another place i am passing in a List<Guid> uploads as a parameter and the list of guids is like: List<Guid?> guidID = new List<Guid?>(); guidID.Add(new Guid("48A583CA-A532-419A-9CDB-292764CEC541")); how can i loop through them and extract each one to individual pass each guid into a stored procedure in a loop?
The same way - simply use a for each loop on the List to get each Guid in that list. Though in this case since the List has Guid?, you'll need to check for null, and you'll also need (most likely) to convert the Guid to a string (string g = new Guid(stringValue); would be one way).
Would be possible if you can add the structure for looping through the guids and assigning the list into a string?
Do you want to make one string out of all the Guids in the List? In that case, you can use String.Join with the separator of your choice on the List.
string newGuid = new Guid(g) tells me that there are invalid arguments. should i add a .ToString() at the end?
|
2

You can't use local names of elements, because you have namespace declared. So, you should use namespace to provide names:

XNamespace ns = "http://schemas.acatar.com/2013/03/Malt.Models";
var guids = from r in xdoc.Root.Elements(ns + "Record")
            select Guid.Parse((string)r.Element(ns + "TaskGUID"));

Or query your xml without specifying names of elements:

var guids = xdoc.Root.Elements()
                .Select(r => Guid.Parse((string)r.Elements().Single()));

3 Comments

what does xdoc stand for? XDocument?
@loop852 yes, var xdoc = XDocument.Parse(xml)
@lazyberezovsky - you're missing a closing ) at the end of your second query :) The third ) will close at the Select.
0

I think this is either what you are after or perhaps might shed some light on the direction to go:

string xml = "";  // XML data here

XDocument doc = XDocument.Parse(xml);

List<Guid> guids = doc.Descendants("TaskGUID")
                         .Select(g => new Guid(g.Value))
                         .ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.