Posts Tagged Data Formats

Visual Studio 2010 XML Schema Errors–xsd:import


Introduction

I’ve been looking at the option for a Publish and Subscribe web service model for a development project which is starting up at work. The research into the Publish and Subscribe model has had me looking at closely at the WS-BaseNotification specification.

To take a closer look at the standard, and what would be involved in implementing this for the project I downloaded the xml schemas (XSD’s) and web service definition language (WSDL) files for the standard. These files I then loaded up into Visual Studio 2010, and that’s when the “fun started”. These files were including (xsd:import) schemas from over the web, and Visual Studio was getting errors doing this. After a quick browse on the internet, it seemed like a common problem, with some complex solutions.  So, thinking there must be a better way, I started solving the problem, my way.

The Errors

Warning          2          Request for the permission of type ‘System.Net.WebPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.

Warning          3          Request for the permission of type ‘System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.

The “Fix”

I have found that the simplest way to solve these types of errors is to do the following:

  • Download the xsd file being referenced.
    • Paste the URL into IE and get the xsd file up in the browser.
    • Use File –> Save As. To save a copy of the xsd file into the Visual Studio project directory which you are keeping your source code in.
  • Add the xsd file you just downloaded to the project, and open the xml editor for that file.
  • Right click the editor tab for file you just downloaded, and select the “Copy Full Path”.
  • Then change the xsd:import from:
  <xsd:import namespace="http://www.w3.org/2005/08/addressing"
    schemaLocation="http://www.w3.org/2005/08/addressing/ws-addr.xsd"
  />

To

  <xsd:import namespace="http://www.w3.org/2005/08/addressing"
    schemaLocation="file://visual studio 2010/Projects/Project1/DummyShell/ws-addr.xsd"
  />

  • Tip: You have the file location in the clipboard from the “Copy Full Path”, so you only need to swap the \’s to /’s.
  • This makes the reference to a local copy of the schema file.

I’ve had situations where Visual Studio still gives the System.Security.Permissions.FileIOPermission exception/error after making this change. Restarting Visual Studio seemed to “clean out” the error and everything was fine after the restart.

Conclusion

Importing schemas into other schemas is a very powerful feature of xml schema language, but a real pain with Visual Studio. I’m not sure why things get so difficult, the schema cache which Visual Studio maintains seems to, in part, cause extra confusion. This approach ignores the schema cache altogether. Although I did “copy” the xml schema (xml.xsd) from the cache, into the project as well (one of the consequential dependencies required it).

Advertisements

, , , , , , ,

7 Comments

LINQ to XML: using let, yield return and SelectMany


Introduction

This blog post continues my series on LINQ. The preceding editions which cover some of my experiences with LINQ are list blow, or to the right.

This addition to the LINQ series probably qualifies in the “cruel and unusual” things which can be done with LINQ category. The techniques which are listed here are probably uses of LINQ which fall into the “corner cases” or “edge cases” of LINQ usage.

What is in the C# Source Code Below

There are a couple of things which this code shows:

  • The use of the LINQ let clause. This has proved to be a very convenient way of taking the “bulk” out of the LINQ to XML expressions, and is something which could be seen as an application of the “DRY principle”. I’ve always had the feeling that LINQ to XML was very good for creating XML documents, but was a bit on the verbose side when it came to interrogating XML. Judicious use of the let clause seems like a way to knock some of the verbosity out of the LINQ to XML.
  • There is another benefit of the judicious use of the let clause, and that is that you can imbed complex LINQ expression into LINQ statements in a convenient way. The following code segment shows let used for simple, and one complex (the InfoLines) expressions. The InfoLines is string concatenation from multiple lines.
    var question3 = (from Nodes in XDocument.Load(nodeFileName).Elements("InformationGraph").Elements("Node")
                     from ToElements in Nodes.Elements("Tos").Elements("Entity")
                     from FromElements in Nodes.Elements("Froms").Elements("Entity")
                     let InfoLines = (from InfoLines in Nodes.Elements("InformationContent").Elements("InformationConentLine")
                                      select InfoLines.Value)
                                      .DefaultIfEmpty("No information Content Recorded")
                                      .Aggregate((current, next) => current + ", " + next)
                     let ToElement = ToElements.Element("EntityName").Value
                     let ToObjType = ToElements.Element("EntityObjectType").Value
                     let ToSubSystem = ToElements.Element("SubSystem").Value
                     let FromElement = FromElements.Element("EntityName").Value
                     let FromObjType = FromElements.Element("EntityObjectType").Value
                     let FromSubSystem = FromElements.Element("SubSystem").Value
                     select MakeElements(FromElement, FromSubSystem, FromObjType, ToElement, ToSubSystem, ToObjType, InfoLines))
                    .SelectMany(A => A);
  • The above code sample also introduces another  interesting use of LINQ. This one is the MakeElements function (the code for which follows). This function demonstrates how to produce more than one output (XElement in this case) from a single input. The .SelectMany unwraps the sequence which the MakeElements function generates, into a single level sequence.
    private static IEnumerable<XElement> MakeElements(string FromEntity, string FromSubSystem, string FromObjType,
                                                      string ToEntity, string ToSubSystem, string ToObjectType,
                                                      string LinkTypeDescription)
    {
        string TargetQ = MakeNodeID(ToEntity);
        string SourceQ = MakeNodeID(FromEntity);
        string SourceSubSysQ = String.Empty;
        string TargetSybSysQ = String.Empty;
        yield return new XElement("Node",
            new XAttribute("ID", SourceQ),
            new XAttribute("Label", FromEntity),
            new XAttribute("Category", FromObjType));
        yield return new XElement("Node",
            new XAttribute("ID", TargetQ),
            new XAttribute("Label", ToEntity),
            new XAttribute("Category", ToObjectType));
    
        if (!String.IsNullOrEmpty(FromSubSystem))
        {
            SourceSubSysQ = MakeNodeID(FromEntity + "::" + FromSubSystem);
            yield return new XElement("Node",
                new XAttribute("ID", SourceSubSysQ),
                new XAttribute("Label", FromSubSystem),
                new XAttribute("Category", "SubSystem"));
            yield return new XElement("Link",
                new XAttribute("Source", SourceQ),
                new XAttribute("Target", SourceSubSysQ),
                new XAttribute("Label", "Sub System"));
            SourceQ = SourceSubSysQ;
        }
        if (!String.IsNullOrEmpty(ToSubSystem))
        {
            TargetSybSysQ = MakeNodeID(ToEntity + "::" + ToSubSystem);
            yield return new XElement("Node",
                new XAttribute("ID", TargetSybSysQ),
                new XAttribute("Label", ToSubSystem),
                new XAttribute("Category", "Subsystem"));
            yield return new XElement("Link",
                new XAttribute("Source", TargetQ),
                new XAttribute("Tagert", TargetSybSysQ),
                new XAttribute("Label", "Sub System"));
            TargetQ = TargetSybSysQ;
        }
        yield return new XElement("Link",
            new XAttribute("Source", SourceQ),
            new XAttribute("Target", TargetQ),
            new XAttribute("Label", LinkTypeDescription));
    }
    • The above function demonstrates another interesting “abuse” of the yield return statement. In this case, using the yield return to emit an XElement into the output sequence. This a approach seems to work very well. Also, please note: that the DGML elements which are being generated need to have the DGML namespace added to the XName (http://schemas.microsoft.com/vs/2009/dgml ) (that one came to light today as I was using the output from this function).
    • The following code segment is the MakeNodeID function. This is yet another demonstration of the powers of LINQ to do manipulations on a string object.
      private static String MakeNodeID(string NameString)
      {
          return new String(NameString.Where(A => !Char.IsPunctuation(A) && !Char.IsSeparator(A)).ToArray());
      }

    Conclusions

  • This above code samples are just fragments from the systems I’m building which generates DGML from an XML file. The XML contains information about the relationships between systems, internal and external entities in the organisation I work for. This approach seems to be panning out quite well. I’ll include some more of the “interesting” code from this development in subsequent blog posts. I have an interesting set of LINQ statements which pulls a big DGML graph apart into smaller graphs.

    , , , , , , , , , ,

    1 Comment

    Yield Return: Uses, Abuses and “Rules of Thumb”


    Introduction

    I’ve been exploring (and exploiting) the possibilities which the “yield return” statement presents in the C# language. The “yield return” and LINQ provide a very powerful combination in the C# d language.  The power of the yield return has been something which I’ve not “lent” upon, until this week. This week I’ve been doing some “fancy dancing’” in the program I’m writing, and yield return start being the way to solve some tricky logic problems. This program is one which is generating multiple DGML graphs (files) from the one input file. The yield return I’ve been using to generate the pagination of the input data into multiple output DGML graphs.

    What does yield return do?

    My explanation of what yield return does is quite simple. It is a method which emits one element into the sequence (the IEnumerable) at a time. The timing of the emission of the being “governed” by the iteration over the sequence.

    The previous statements being taken on face value, why does yield return have some constraints on its use? If you read the references below, or the MSDN pages on yield return, you will see that there are a number of constraints on the placement of yield return statements. These constraints are below (a straight copy and paste from the MSDN online documentation):

    The yield statement can only appear inside an iterator block, which can be implemented as the body of a method, operator, or accessor. The body of such methods, operators, or accessors is controlled by the following restrictions:

    • Unsafe blocks are not allowed.
    • Parameters to the method, operator, or accessor cannot be ref or out.
    • A yield return statement cannot be located anywhere inside a try-catch block. It can be located in a try block if the try block is followed by a finally block.
    • A yield break statement may be located in a try block or a catch block but not a finally block.

    A yield statement cannot appear in an anonymous method.

    So what do the constraints mean for a programmer using the yield return. In my opinion the constraints are not to hard to live with. The constraint on out or ref parameters makes sense when one considers what is calling the method, which is part of the iteration infrastructure “baked-into” the C# language. If your interested in the design decisions which went into the implementation of this feature of C# have a read of the links to the Eric Lippert links at the end of this post. For an explanation of how C# implements this feature read the Jon Skeet article at the end of this post (the explanation covers all of the gorey details on the implementation).

    What the following examples try and demonstrate

    The following examples are just a couple of ways that the “yield return” can be used. There are a couple of features which are worthwhile noting:

    • These examples use multiple “yield return” statements in the one method. This  is something which the example code I’ve looked at does not include. A feature which I’ve found very useful.
    • The methods all return IEnumerable of type which is the “standard” object which LINQ is defined over the top of (as extension methods on the IEnumerable of type).
    • There are local variables included in the methods. These work just as one would expect.
    • The methods can result in more than one output value being emitted into the resulting sequence. The ability to generate more than one output value for one input value is one of the big benefits of this approach. The comparison approach in LINQ becomes very complex very quickly, with multiple joins and/or multiple unions.
    • using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      namespace YieldReturn
      {
          /// <summary>
          /// Stub class used in the examples
          /// </summary>
          class EntityDetails
          {
              public string EntityType, Name, SubSystem;
          }
          /// <summary>
          /// Stub class used in the examples
          /// </summary>
          class DGML_XML_Assembler
          {
          }
          /// <summary>
          /// Dummy class which is used in the demonstration of yield return
          /// </summary>
          class BulkPagenation
          {
              private DGML_XML_Assembler writer;
              internal bool Matches(string Name, string SubSystem, string Type)
              {
                  throw new NotImplementedException();
              }
              public DGML_XML_Assembler Writer
              {
                  get { return this.writer; }
              }
          }
          /// <summary>
          /// Class which contains some examples of yield return
          /// </summary>
          class Program
          {
              private List<BulkPagenation> BulkPages;
              static void Main(string[] args)
              {
              }
              /// <summary>
              /// Example of a more complex lambda expression
              /// </summary>
              private void experiments()
              {
                  List<String> strList = new List<string>();
                  var a = strList.Where(A =>
                  {
                      switch (complex1(A))
                      {
                          case 1: return true;
                          default: return false;
                      }
                  }
                  );
      
              }
              /// <summary>
              /// function called by the above example
              /// </summary>
              /// <param name="value"></param>
              /// <returns></returns>
              private int complex1(string value)
              {
                  return 1;
              }
              /// <summary>
              /// Example which shows using a Union to form a complex result
              ///
              ///
      ToEntities">
              ///
      
              private void Sample1a(List<EntityDetails> ToEntities, List<EntityDetails> FromEntities)
              {
                  foreach (EntityDetails xx in ToEntities.Where(A => A.EntityType == "InternalSystem")
                      .Union(FromEntities.Where(A => A.EntityType == "InternalSystem")))
                  {
                  }
              }
              /// <summary>
              /// Another way of expressing the Union of two result sets
              ///
              ///
      ToEntities">
              ///
      
              private void Sample1b(List<EntityDetails> ToEntities, List<EntityDetails> FromEntities)
              {
                  var a = ToEntities.Where(A => A.EntityType == "InternalSystem");
                  var b = FromEntities.Where(A => A.EntityType == "InternalSystem");
                  foreach (EntityDetails xx in a.Union(b))
                  {
                  }
              }
      
              /// <summary>
              /// Example consuming a yield return implementation
              /// 
              ///
      
              ///
      FromEntities">
              private void Sample2(List<EntityDetails> ToEntities, List<EntityDetails> FromEntities)
              {
                  foreach (EntityDetails Entity in FindTheInternalSystems(ToEntities, FromEntities))
                  {
                  }
              }
              /// <summary>
              /// Implementation which generates a sequence using yield return
              /// </summary>
              /// <param name="ToEntities"></param>
              /// <param name="FromEntities"></param>
              /// <returns></returns>
              private IEnumerable<EntityDetails> FindTheInternalSystems(List<EntityDetails> ToEntities, List<EntityDetails> FromEntities)
              {
                  foreach (EntityDetails entity in ToEntities)
                  {
                      if (entity.EntityType == "InternalSystem")
                          yield return entity;
                  }
                  foreach (EntityDetails entity in FromEntities)
                  {
                      if (entity.EntityType == "InternalSystem")
                          yield return entity;
                  }
              }
      
              /// <summary>
              /// A more complex example where the generated sequence has more than one matching input
              /// </summary>
              /// <param name="Name"></param>
              /// <param name="SubSystem"></param>
              /// <param name="Type"></param>
              /// <returns></returns>
              private IEnumerable<DGML_XML_Assembler> GetAssemblersFor(string Name, string SubSystem, string Type)
              {
                  bool used = false;
                  foreach (BulkPagenation page in this.BulkPages)
                  {
                      if (page.Matches(Name, SubSystem, Type))
                      {
                          used = true;
                          yield return page.Writer;
                      }
                  }
                  if (!used)
                  {
                      var bal = this.BulkPages.Where(A => A.Matches("Balance", String.Empty, "SystemTriggers"));
                      if (bal.Any())
                      {
                          yield return bal.First().Writer;
                      }
                  }
                  var all = this.BulkPages.Where(A => A.Matches("All", String.Empty, "SystemTriggers"));
                  if (all.Any())
                  {
                      yield return all.First().Writer;
                  }
              }
      
          }
      }

    Multiple outputs for one input in LINQ

    LINQ has in the pedigree which comes from some of the same root concepts as SQL. These roots results in SQL being great powers for sub-setting data, being inherited by LINQ. But, apart from joining 2 tables in SQL, or using Union, you cannot generate multiple output rows from an input row. LINQ has a similar relational algebra underpinning as SQL, and inherits the same sort of constraint on generating multiple rows.

    Rules of Thumb

    There are some things which you cannot easily do with straight LINQ that can be done with the “yield return” statement.

    Having a method which is generating the sequence on demand is another feature which makes using the construct more effective and flexible than just LINQ operations.

    My rules of thumb (the rough and ready guidance I’ve gleaned to date) for when you should consider using the “yield return” method:

    • If your selection logic is complex. This is a bit arbitrary, but when it “feels like it won’t fit” in a lambda expression then consider using the “yield return”.
    • If you want to generate more than one output element from one input element. Sure, the Union LINQ extension method can be used to stitch together two sequences. This works for a small number of sequence, but as the of sequence number climbs it would start to get unwieldy.
    • If you have complex join conditions. Some of the above examples could be expressed as joins between the two sequences, with a conditional join condition. A “conditional join condition” is a bit of a mouthful, really it is a multiple case join condition, (if condition a, join one way, else, join another way).

    Conclusions

    The wise application of the “yield return” in C# developments is something which can improve the clarity of an implementation. It is well worth a C# developers time mastering.

    This construct “hurt my brain” when I came to it. That is probably because I  had read a “bit too much” about the inner mechanics of the implementation in the C# language. Having too much of a focus on the inner workings, rather than focusing on what the technique could offer to my C# developments, was a mistake which probably made coming to terms with the technique.

    References

    Previous LINQ Blog Posts

    , , , , , , , , , ,

    Leave a comment

    %d bloggers like this: