tags:

views:

49

answers:

2

All, I'm new to Linq to XML and have a question on how to generate XML, based on the following format prescribed by a vendor, from a List<string> containing the names of nested directories. - Please help. Thanks

Vendor format:

<eccu>
  <match:recursive-dirs value="store" >  
    <match:recursive-dirs value="images" >  
       <revalidate>now</revalidate>  
    </match:recursive-dirs>  
  </match:recursive-dirs> 
</eccu>

Here's my code. However as you can see it does not produce the correctly formatted results.:

// NOTE - textBox1.Text = 'http://www.someurl.com/dir1/di2/images'
    var dirs = (new Uri(textBox1.Text)).Segments.Select(dir => dir.Replace("/", String.Empty)).Where( dir => !String.IsNullOrWhiteSpace(dir)).ToList();
    var x = new XDocument(new XDeclaration("1.0", null, null), new XElement("eccu", from s in dirs select new XElement("recursive-dirs", new XAttribute("value", s)), new XElement("revalidate", "now")));

This produces:

<eccu>
  <recursive-dirs value="dir1" />
  <recursive-dirs value="dir2" />
  <recursive-dirs value="images" />
  <revalidate>now</revalidate>
</eccu>
A: 

Something like this should do it in a hard coded fashion, assuming match:recursive-dirs is valid xml (I don't know). Since you are talking about a list strings, I will need to see their format to work up a LINQ statement.

XElement xml = new XElement("eccu",
                   new XElement("match:recursive-dirs", 
                       new XAttribute("value", "store"),
                           new XElement("match:recursive-dirs", 
                                new XAttribute("value", "images"),
                                new XElement("revalidate", "now")
                          )
                       )
                   )
               );

based on HookedOnLink.com

Edit based on comments
It's not as pretty, but

string text = "http://www.someurl.com/dir1/di2/images";
var dirs = (new Uri( text )).Segments
                            .Select( dir => dir.Replace( "/", String.Empty ) )
                            .Where( dir => !String.IsNullOrWhiteSpace( dir ) )
                            .ToList( );

var x = new XDocument(
            new XDeclaration( "1.0", null, null ), 
                new XElement( "eccu" ) );

var eccu = x.Elements( ).First( );
XElement current = eccu;

foreach( var dir in dirs )
{
    XElement newXElement = new XElement( "recursive-dirs", 
                           new XAttribute( "value", dir ) );
    current.Add( newXElement );
    current = newXElement;
}

current.Add( new XElement( "revalidate", "now" ) );     
Console.Out.WriteLine( x.ToString());

produces

<eccu>
  <recursive-dirs value="dir1">
    <recursive-dirs value="di2">
      <recursive-dirs value="images">
        <revalidate>now</revalidate>
      </recursive-dirs>
    </recursive-dirs>
  </recursive-dirs>
</eccu>
BioBuckyBall
Hard coding is not an option. The list of directories could be 1 or it could be 10 layers deep. The number depends on how many segments are parsed out of a particular URL. Additionally, haveing the ':' is not valid. I'm trying to figure that one out too.
MarcS
@MarcS I didn't expect you would want a hard coded solution, but that shows you how to create an object graph that looks like you want. Now that you have added some detail to your question I will readdress your specific request in an edit.
BioBuckyBall
Thanks for your attention to my question. This stuff can be pretty complicated.
MarcS
BTW - While my code snip attempts to do everything in a single line of code, there is no specific requirement to do so.
MarcS
Thanks. That works. now I just need to get past the ":" issue. The vendor requires a colon ':' as such <match:recursive-dirs value=>. This one is stumping me too.
MarcS
Question: When I inspect x.ToString() it has everything except for the declaration. Is there another method I should call to get that as well? The strange part is when I save the XML to a text file, it has it in there. I'm going to be passing, via web service, the XML to a vendor.
MarcS
@MarcS I don't know. Sounds like an idea for a question on stackoverflow :)
BioBuckyBall
+1  A: 

Your XML is slightly incorrect as it needs to define the namespace "match". The following code will produce XML with the namespace attribute correctly defined

XNamespace aw = "http://www.adventure-works.com";
        XElement root = new XElement(aw + "Root",
            new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
            new XElement(aw + "Child", "content")
        );

which produces

<aw:Root xmlns:aw="http://www.adventure-works.com"&gt;&lt;aw:Child&gt;content&lt;/aw:Child&gt;&lt;/aw:Root&gt;

(taken from http://msdn.microsoft.com/en-us/library/system.xml.linq.xnamespace.xmlns.aspx) You'll need to adjust your code accordingly.

RogerNoble
there we go...I'm not that knowledgable when it comes to xml.
BioBuckyBall
Thanks very much for everyone's help. - Marc
MarcS