Search code examples
c#xmlxml-serializationxml-formatting

Structuring XDocument to properly format Tags


I have an XDocument being created that gets populated with a set of data.

The output looks like so:

    <Results>
      <RuleResult>
        <Title> RuleTitle </Title>
      </RuleResult>
      <RuleResult>
        <Title> Rule2Title </Title>
      </RuleResult>
    </Results>

now how I have this formulated in C# is as follows:

    XDocument doc = new XDocument(new XElement("Results"));
    foreach (AnalysisSet rules in allAnalysisSets)
     {
       foreach (var rule in rules.Rules)
        {
          doc.Root.Add(new XElement(rule.GetRuleState()));
        }
     }

To my understanding, this creates "Results" as the root level node.

My question is, if I want to set it up so that encapsulating all of the above is <AnalysisSets> so it would be:

    <AnalaysisSets>
      <AnalysisSet ProviderName="ProductNameHere">
        <Results>
          <....xml data..../>
        </Results>
      </AnalysisSet>
    </AnalysisSets>

How would I do this? It seems like I would be trying to create a Root element, and then 2 sub root elements? I am not quite sure how to go about accomplishing that, if that is indeed the right track to begin with.


Solution

  • Have you considered just using straight up serialization? Anyway here is my complete mockup.

    Edit: I forgot the Results node :-)

    public class Program
    {
        private static void Main(string[] args)
        {
            var allAnalysisSets = new AnalysisSet[] {
                new AnalysisSet(){
                    ProviderName = "productname1",
                    Rules = new Rule[]{
                    new Rule(){ Title="rule1" }
                } },
                new AnalysisSet(){
                    ProviderName = "productname2",
                    Rules = new Rule[]{
                    new Rule(){ Title="rule1" },
                    new Rule(){ Title="rule2" }
                } }
            };
    
            var sb = new StringBuilder();
            using (var sw = new StringWriter(sb))
            using (var xw = XmlWriter.Create(sw, new XmlWriterSettings() { Indent = true }))
            {
                new XElement("AnalaysisSets"
                    , allAnalysisSets
                        .Select(set => new XElement("AnalysisSet"
                                , new XAttribute("ProviderName", set.ProviderName)
                                , new XElement("Results"
                                    , set.Rules.Select(rule => rule.GetRuleState())
                                )
                            )
                        )
                ).WriteTo(xw);
            }
            Console.Write(sb.ToString());
            Console.ReadLine();
        }
    }
    
    public class AnalysisSet
    {
        public IEnumerable<Rule> Rules { get; set; }
    
        public string ProviderName { get; set; }
    }
    
    public class Rule
    {
        public string Title { get; set; }
    
        public XStreamingElement GetRuleState()
        {
            return new XStreamingElement("RuleResult",
                new XElement("Title", Title));
        }
    }