开发者

Need help with LINQ to XML

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 XElemen开发者_如何学Ct("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>


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"><aw:Child>content</aw:Child></aw:Root>

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜