Masriyah
Masriyah

Reputation: 2505

Parsing xml string to get certain tag values within

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

Upvotes: 0

Views: 585

Answers (3)

Tim
Tim

Reputation: 28530

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
    }
}

Upvotes: 2

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

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()));

Upvotes: 2

Bazzz
Bazzz

Reputation: 26922

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();

Upvotes: 0

Related Questions