Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Join files

Tags:

c#

linq-to-xml

My code is as under

int cnt =  ScriptInfoList.Count;
for (int i = 0; i < cnt; i++)
{
               var value =  PrepareXMLDocument(ScriptInfoList[i]);
}

private static XDocument PrepareXMLDocument(ScriptInfo scriptInfo)
{

            XDocument doc =
                         new XDocument(
                           new XElement("scriptfilenames",
                               new XElement("SqlEye",
                                   new XElement("scriptfilename", new XAttribute("Name", scriptInfo.FileName), new XAttribute("Type", scriptInfo.ScriptType),
                                       new XElement("SqlEyeWarnings",
                                        sqlEyeWarnings.Select(x => new XElement("SqlEyeWarning", new XAttribute("value", x)))),
                                        new XElement("FxCopsWarnings",
                                        fxCopWarnings.Select(x => new XElement("FxCopsWarning", new XAttribute("value", x)))),
                                        new XElement("SqlEyeRemarks",
                                        sqlEyeRemarks.Select(x => new XElement("SqlEyeRemark", new XAttribute("value", x)))),
                                        new XElement("FxCopsRemarks",
                                        fxCopRemarks.Select(x => new XElement("FxCopsRemark", new XAttribute("value", x))))
                                        ))));
            return doc;
}

Hoe can I merge mutilpe XDocuments?

As sample is

File1

<scriptfilenames>
  <SqlEye>
    <scriptfilename Name="ws_CallLogs_GetByCallId.sql" Type="SP">
      <SqlEyeWarnings>
        <SqlEyeWarning value="SD030:  object does not exist in database or is invalid for this operation in Database   : ws_CallLogs  @ line number : 63" />        
      </SqlEyeWarnings>
      <FxCopsWarnings>
        <FxCopsWarning value="Avoid using sp_ as a prefix for stored procedure " />        
      </FxCopsWarnings>
      <SqlEyeRemarks>
        <SqlEyeRemark value="SP017: Consider using EXISTS predicate instead of IN predicate  @ line number : 1" />
      </SqlEyeRemarks>
      <FxCopsRemarks>
        <FxCopsRemark value="Missing or order mismatch of Grant statement." />
      </FxCopsRemarks>
    </scriptfilename>
  </SqlEye>
</scriptfilenames>

File2

<scriptfilenames>
  <SqlEye>
    <scriptfilename Name="dbo.StopAutoRenewalEx.StoredProcedure.sql" Type="SP">
      <SqlEyeWarnings />
      <FxCopsWarnings>       
        <FxCopsWarning value="Missing schema while addressing object name" />
      </FxCopsWarnings>
      <SqlEyeRemarks>
        <SqlEyeRemark value="SP016: Update statements should not update primary key  @ line number : 70" />        
      </SqlEyeRemarks>
      <FxCopsRemarks>
        <FxCopsRemark value="Values hardcoded in where-clause condition " />
        <FxCopsRemark value="Values hardcoded in where-clause condition " />
        <FxCopsRemark value="Values hardcoded in where-clause condition " />
      </FxCopsRemarks>
    </scriptfilename>
  </SqlEye>
</scriptfilenames>

The merged will be

<scriptfilenames>
  <SqlEye>
    <scriptfilename Name="ws_CallLogs_GetByCallId.sql" Type="SP">
      <SqlEyeWarnings>
        <SqlEyeWarning value="SD030:  object does not exist in database or is invalid for this operation in Database   : ws_CallLogs  @ line number : 63" />        
      </SqlEyeWarnings>
      <FxCopsWarnings>
        <FxCopsWarning value="Avoid using sp_ as a prefix for stored procedure " />        
      </FxCopsWarnings>
      <SqlEyeRemarks>
        <SqlEyeRemark value="SP017: Consider using EXISTS predicate instead of IN predicate  @ line number : 1" />
      </SqlEyeRemarks>
      <FxCopsRemarks>
        <FxCopsRemark value="Missing or order mismatch of Grant statement." />
      </FxCopsRemarks>
    </scriptfilename>

    <scriptfilename Name="dbo.StopAutoRenewalEx.StoredProcedure.sql" Type="SP">
      <SqlEyeWarnings />
      <FxCopsWarnings>       
        <FxCopsWarning value="Missing schema while addressing object name" />
      </FxCopsWarnings>
      <SqlEyeRemarks>
        <SqlEyeRemark value="SP016: Update statements should not update primary key  @ line number : 70" />        
      </SqlEyeRemarks>
      <FxCopsRemarks>
        <FxCopsRemark value="Values hardcoded in where-clause condition " />
        <FxCopsRemark value="Values hardcoded in where-clause condition " />
        <FxCopsRemark value="Values hardcoded in where-clause condition " />
      </FxCopsRemarks>
    </scriptfilename>
  </SqlEye>
</scriptfilenames>

My solution is

StringBuilder sb = new StringBuilder();
            sb.AppendLine("<scriptfilenames><SqlEye>");
            int cnt =  ScriptInfoList.Count;

            for (int i = 0; i < cnt; i++)
            {
               var value =  PrepareXMLDocument(ScriptInfoList[i]);
               var findContent = value.Descendants("scriptfilename");
               sb.AppendLine(value.Descendants("scriptfilename").ToList()[0].ToString());
            }

            sb.AppendLine("</SqlEye></scriptfilenames>");

Please provide a better answer

like image 483
priyanka.sarkar Avatar asked Feb 01 '26 20:02

priyanka.sarkar


2 Answers

Use LINQ to XML to select the list of <scriptfilename> elements and add them to a new XDocument:

var xmls = new List<XDocument>
{
    XDocument.Load("File1.xml"),
    XDocument.Load("File2.xml")
};

var resultXml = new XDocument(
    new XElement("scriptfilenames",
        new XElement("SqlEye",
            xmls.Descendants("scriptfilename"))));
like image 124
Fung Avatar answered Feb 03 '26 09:02

Fung


You can just load the 2 xml files in 2 documents and then in one of the document, you can get the <SqlEye> element and append the document 2's DocumentElement as the child of the <SqlEye> in the 1st xml document.

like image 34
Saravanan Avatar answered Feb 03 '26 09:02

Saravanan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!