简体   繁体   中英

Parse xml to CLR type using Linq

I have following xml structure and I want to convert this xml into CLR object,but I am stuck.

<?xml version="1.0" encoding="utf-8"?>
<SymbolData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            assembly="C:\adCenter\debug\DLLs\Microsoft.Advertiser.CampaignManagement.BusinessRules.dll">
  <sourceFiles>
    <document id="1" 
              url="d:\7018\4579\Sources\Live\Microsoft.Advertiser.CampaignManagement.BusinessRules\AdExtensionValidators\AdExtensionValidationBatchErrors.cs" 
              language="3f5162f8-07c6-11d3-9053-00c04fa302a1" 
              languageVendor="994b45c4-e6e9-11d2-903f-00c04fa302a1" 
              documentType="5a869d0b-6611-11d3-bd2a-0000f80849bd" />    
    <document id="2" 
              url="d:\7018\4579\Sources\Live\Microsoft.Advertiser.CampaignManagement.BusinessRules\AdExtensionValidators\AdExtensionValidationBatchErrors.cs" 
              language="3f5162f8-07c6-11d3-9053-00c04fa302a1" 
              languageVendor="994b45c4-e6e9-11d2-903f-00c04fa302a1" 
              documentType="5a869d0b-6611-11d3-bd2a-0000f80849bd" />        
  </sourceFiles>
  <method name="Microsoft.Advertiser.CampaignManagement.BusinessRules.AdExtensionValidators.AdExtensionValidationBatchErrors::.ctor" 
          token="0x600000c">
    <sequencePoints>
      <seqPoint ilOffset="0" sourceId="1" startRow="10" startColumn="9" endRow="10" endColumn="106" />
      <seqPoint ilOffset="11" sourceId="1" startRow="12" startColumn="9" endRow="12" endColumn="136" />
      <seqPoint ilOffset="22" sourceId="1" startRow="14" startColumn="9" endRow="14" endColumn="181" />
      <seqPoint ilOffset="33" sourceId="1" hidden="true" startRow="16707566" startColumn="0" endRow="16707566" endColumn="0" />
    </sequencePoints>
    <rootScope implicit="true" startOffset="0" endOffset="40">
      <scope startOffset="0" endOffset="40" isReconstructedDueToDiasymreaderBug="true" />
    </rootScope>
    <symAttributes />
    <csharpCustomDebugInfo version="4">
      <entries>
        <usingForward version="4" tokenToForwardTo="0x6000001" />
      </entries>
    </csharpCustomDebugInfo>
  </method>
  <method name="Microsoft.Advertiser.CampaignManagement.BusinessRules.AdExtensionValidators.AdExtensionValidationError::ToString" token="0x600000f">
    <sequencePoints>
      <seqPoint ilOffset="0" sourceId="2" startRow="167" startColumn="13" endRow="167" endColumn="110" />
    </sequencePoints>
    <rootScope implicit="true" startOffset="0" endOffset="33">
      <scope startOffset="0" endOffset="33" isReconstructedDueToDiasymreaderBug="true" />
    </rootScope>
    <symAttributes />
    <csharpCustomDebugInfo version="4">
      <entries>
        <usingForward version="4" tokenToForwardTo="0x600000d" />
      </entries>
    </csharpCustomDebugInfo>
  </method>


</SymbolData>

I have written following code to achive the same,but I am not able to query method section . For example,I want to load all methods where documentID and SourceID is same.

public class ClassDetails
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }
    public class AssemblyDetails
    {
        public string DllName { get; set; }
        public List<ClassDetails> Classes { get; set; }
        public List<string> Methods { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var document = XDocument.Load("test11.xml");
            var query = from descendantNode in document.Descendants("sourceFiles")
                        select new AssemblyDetails()
                        {
                            DllName = document.Root.Attribute("assembly").Value,
                            Classes = (from x in document.Descendants("document")
                                      select new ClassDetails
                                      {
                                          Id=x.Attribute("id").Value,
                                          Name=x.Attribute("url").Value
                                      }).ToList(),
                            Methods = (from y in document.Descendants("method").Elements("seqPoint")
                                     where y.Attribute("sourceId").Value=="documentID"
                                     select y.Document.Root.Attribute("name").Value).ToList()
                       };

            int i=10;
        }
XDocument xdoc = XDocument.Load(path_to_xml);
var documentIds = xdoc.Descendants("document")
                      .Select(d => (int)d.Attribute("id"));
                      .ToList();

var methodNames = xdoc
    .Descendants("method")
    .Where(m => m.Descendants("seqPoint")
                 .Any(sp => documentIds.Contains((int)sp.Attribute("sourceId"))))
    .Select(m => (string)m.Attribute("name"));

First query gets all document ids.

Second query gets method names of methods, which have at least one seqPoint with sourceId of specified documents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM