How do I get LinqToXSD to properly output namespace prefix declarations?

前端 未结 1 687
清歌不尽
清歌不尽 2021-02-09 01:41

I am experimenting creating XML data binding classes with LinqToXSD and an XML Schema containing a number of imported schemas. All of the schemas are located here.

1条回答
  •  无人共我
    2021-02-09 02:14

    You should map each of the imported schemas:

    
        
    

    elementFormDefault only applies to the schema that it is in and it doesn't override settings in any include or imports.

    If you want to hide namespaces then all schemas must specify elementFormDefault="unqualified". Similarly if you want to expose namespaces every schema must specify elementFormDefault="qualified"

    UPDATED after reviewing unit tests:

    Your input:

    
        My Project 
        2012-05-15 
    

    Your output:

     
        
            My Project 
            2012-05-15 
    

    The outstanding problem is the duplication of the tag - everything looks ok to me, still struggling to understand why this is happening.

    UPDATE Monday:

    I think there is a bug in the LinqToXSD tool - I've run through every combination I can think of and can't consistently work around your problem, however... I have managed to fix the duplication issue:

    In your XmlHelper file, change the return statement:

    System.Xml.Linq.XDocument xd = System.Xml.Linq.XDocument.Parse(sb.ToString());
    return xd.Root.FirstNode.ToString();
    

    I know it's a hack, but it fixes the problem and your LoopbackTest passes.

    You don't get any prefixes if you create elements using the Tmats class, I've tried various combinations of attributes and the best I could do was re-attach namespaces. If you are exchanging information with an external system then I do have a fix:

    1. Use your Tmats object in your code,
    2. Serialize it with namespaces,
    3. Run it through an XSLT to map ns to prefixes.

    I know it's clunky, but I reckon it's the best you're going to get short of actually fixing the LinqToXSD code.

    XSLT to map namespaces to prefixes (you need to maintain the set of namespaces in 'stylesheet' declaration and also in the 'mapper':

    
      
      
      
        
        
        
        
        
        
        
        
        
        
        
      
      
         
           
         
     
    
     
      
      
       
       
       
      
     
    
    

    Produces:

    
      My Program
      2012-05-09-07:00
      
        Robert Harvey
        My Agency
        12345 Anywhere Street
        111-222-3333
      
    
    

    Ok, so this is far from ideal, but your code works fine internally to the project it only when you need to interact with other people that you'd need to fix the xml output (remember to change elementFormDefault="qualified" (or remove it) in your XSD) - if you cache the XSLT as a XslCompiledTransform you'd barely notice it happening at all.

    0 讨论(0)
提交回复
热议问题