English 中文(简体)
B. 需要帮助
原标题:Need help with LINQ to XML

所有,I m New to Linq to XML and have a question on how to make XML, based on the following form specified by aaker, from a List<string > including the name of nestedDirectories. - 请提供帮助。 增 编

供应商格式:

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

我的法典就是这样。 然而,由于你能够看到它没有产生正确格式的结果:

// 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")));

结果是:

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

生产

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

采用硬编码方式,如:match:recursive-dirs是有效的xml(我不知道)。 由于你谈论的是名单说明,我需要看到其格式来编写准则说明。

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

生产

<eccu>
  <recursive-dirs value="dir1">
    <recursive-dirs value="di2">
      <recursive-dirs value="images">
        <revalidate>now</revalidate>
      </recursive-dirs>
    </recursive-dirs>
  </recursive-dirs>
</eccu>




相关问题
Updating Linq to XML element values concatenation issues

I am trying to write a app.config / web.config error correcting app which will audit our developers applications for incorrect environment settings. I am using Linq to XML to accomplish this and I am ...

Is LINQ to XML s XElement ordered?

When I use LINQ to XML, is the order of the elements and attributes written out to text guaranteed to be the same order as how I added the XElement and XAttribute objects? Similarly, when I read in ...

热门标签