2

I am trying to select out the "publishInformation" node, (specifically the "Publish_Date" value) from the following xml.

<?xml version="1.0" standalone="yes"?>
    <sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <publshInformation>
        <Publish_Date>04/06/2018</Publish_Date>
        <Record_Count>6297</Record_Count>
    </publshInformation>
    <sdnEntry>
        <uid>36</uid>
        <lastName>TestLastName</lastName>
        <sdnType>Entity</sdnType>
        <addressList>
          <address>
             <uid>25</uid>
             <city>TestCity</city>
             <country>TestCountry</country>
          </address>
        </addressList>
    </sdnEntry>
    </sdnList>

All of my attempts get null results and I am not sure what I am missing. Below is my most recent attempt.

XElement root = XElement.Load(model.InputStream);
XElement publishInformation = root.Descendants("sdnList")
                    .Descendants("publshInformation")
                    .SingleOrDefault();

Using JohnyL's suggestion the code is below.

string xmlstring;
using (StreamReader reader = new StreamReader(model.OfacXml.InputStream))
{
    xmlstring = reader.ReadToEnd();
}

var xml = XElement.Parse(xmlstring);
var dates = xml.Elements("publshInformation").Select(pi => 
    pi.Element("Publish_Date").Value);

I am still getting null in dates with this code.

1 Answer 1

1
var text = 
    @"<?xml version='1.0' standalone='yes'?>
        <sdnList xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
            <publshInformation>
                <Publish_Date>04/06/2018</Publish_Date>
                <Record_Count>6297</Record_Count>
            </publshInformation>
            <sdnEntry>
                <uid>36</uid>
                <lastName>TestLastName</lastName>
                <sdnType>Entity</sdnType>
                <addressList>
                    <address>
                        <uid>25</uid>
                        <city>TestCity</city>
                        <country>TestCountry</country>
                    </address>
                </addressList>
            </sdnEntry>
        </sdnList>";

var xml = XElement.Parse(text);
var dates = xml.Elements("publshInformation").Select(pi => pi.Element("Publish_Date").Value);
dates.ToList().ForEach(WriteLine);

If you'd like to use XDocument instead of XElement:

var xml = XDocument.Parse(text);
var dates = xml.Element("sdnList")
               .Elements("publshInformation")
               .Select(pi => pi.Element("Publish_Date").Value);
Sign up to request clarification or add additional context in comments.

7 Comments

I am still getting a null using XElement and an object instance error when using XDocument. I added the sample code to my question.
I did. My model contains an InputStream and I am converting it to a string before running exactly what you wrote. When I debug, I can see the entire document just fine in the var xml, but for some reason, returns after that are null.
Did you cut off some namespace?
Okay I may be zeroing in on the issue. I have been passing the input stream to my business layer and then trying to take apart the file. I tried putting your code, as well as a few filed tries of my own back into the Controller and it worked. I don't understand. I can see the data in the business layer but can't select it? Also, there were no errors and the references in my project all worked as expected. Am I missing a reference that doesn't trigger an error?
@joerdie It's hard to say without seeing what's going on in your code. If you have this XML as string, there's no problem to parse it with XDocument.Parse or XElement.Parse. If you have a Stream, you can use, then, XDocument.Load or XElement.Load.
|

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.