title: Read XML as a string from inside XML Document
body: <p>I have an XMLDocument which i am deserializing into object in my C# code. It works fine until i have nodes like below,</p>
  
  <pre><code>  &lt;Name&gt;ABC&lt;/Name&gt;
    &lt;Message&gt;Invalid Name&lt;/Message&gt;
  </code></pre>
  
  <p>In the above scenario what i get inside my C# object would be <strong><em>Name = ABC</em></strong> and <strong><em>Message = Invalid Name</em></strong>.</p>
  
  <p>But now i have a requirement to read a complete XML node inside XMLDocument. Which is like this,</p>
  
  <pre><code>&lt;ReadXML&gt;&lt;fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"&gt;
            &lt;entity name="customerproduct"&gt;
              &lt;attribute name="customerproductid" /&gt;
              &lt;attribute name="name" /&gt;
              &lt;attribute name="createdon" /&gt;
              &lt;attribute name="customertype" /&gt;
              &lt;attribute name="custodian_customerproductid" /&gt;
              &lt;attribute name="accountnumber" /&gt;
              &lt;order attribute="name" descending="false" /&gt;
              &lt;filter type="and"&gt;
                &lt;condition attribute="productgroup" operator="eq" uiname="Credit Cards" uitype="productgroup" value="{12344}" /&gt;
                &lt;condition attribute="customer" operator="eq" uiname="abc xyz" uitype="contact" value="{1234}" /&gt;
              &lt;/filter&gt;
            &lt;/entity&gt;
          &lt;/fetch&gt;&lt;/ReadXML&gt;
  </code></pre>
  
  <p>I have an object ReadXML inside my C# code and i was looking to get complete XML that is inside ReadXML node as an string inside my ReadXML variable.</p>
  
  <p>something like this,</p>
  
  <pre><code>string ReadXML = "&lt;fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"&gt;
            &lt;entity name="customerproduct"&gt;
              &lt;attribute name="customerproductid" /&gt;
              &lt;attribute name="name" /&gt;
              &lt;attribute name="createdon" /&gt;
              &lt;attribute name="customertype" /&gt;
              &lt;attribute name="custodian_customerproductid" /&gt;
              &lt;attribute name="accountnumber" /&gt;
              &lt;order attribute="name" descending="false" /&gt;
              &lt;filter type="and"&gt;
                &lt;condition attribute="productgroup" operator="eq" uiname="Credit Cards" uitype="productgroup" value="{12344}" /&gt;
                &lt;condition attribute="customer" operator="eq" uiname="abc xyz" uitype="contact" value="{1234}" /&gt;
              &lt;/filter&gt;
            &lt;/entity&gt;
          &lt;/fetch&gt;"
  </code></pre>
  
  <p>Inside my Code i am doing this,</p>
  
  <pre><code> XmlDocument doc = new XmlDocument();
  
          doc.Load(@"D:\Development\Framework\TestApplication\XMLScript\XMLScript\XMLFile.xml");
  
  
          string XML = doc.InnerXml;
          operationTitle.Text = "Stop Lost Card"; ;
          System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(MyOperation));
          MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(vrp_XML));
          MyOperation = (MyOperation)serializer.Deserialize(memStream);
  </code></pre>
  
  <p>And Inside <strong>MyOperation</strong> Class, i have ReadXML like this,</p>
  
  <pre><code>[XmlElement(ElementName = "ReadXML")]
  public string ReadXML { get; set; }
  </code></pre>
  
  <p>Whenever i try to read the XML into the string i get the following error,</p>
  
  <pre><code>    System.InvalidOperationException was unhandled
    HResult=-2146233079
    Message=There is an error in XML document (1, 1329).
    Source=System.Xml
    StackTrace:
         at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
         at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
         at VeriTouch.CRM.USD.AgentScripting.AgentScriptingControl.loadData() in C:\Projects\AgentScripting_30102018\AgentScripting\AgentScripting\AgentScriptingControl.xaml.cs:line 323
         at VeriTouch.CRM.USD.AgentScripting.AgentScriptingControl.StartButton_Click(Object sender, RoutedEventArgs e) in C:\Projects\AgentScripting_30102018\AgentScripting\AgentScripting\AgentScriptingControl.xaml.cs:line 304
         at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
         at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
         at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
         at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
         at System.Windows.Controls.Primitives.ButtonBase.OnClick()
         at System.Windows.Controls.Button.OnClick()
         at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
         at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
         at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
         at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
         at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
         at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
         at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
         at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
         at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
         at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
         at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
         at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
         at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
         at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
         at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
         at System.Windows.Input.InputManager.ProcessStagingArea()
         at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
         at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
         at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
         at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)
         at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)
         at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean&amp; handled)
         at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
         at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
         at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
         at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
         at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
         at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&amp; msg)
         at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
         at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
         at System.Windows.Application.RunDispatcher(Object ignore)
         at System.Windows.Application.RunInternal(Window window)
         at System.Windows.Application.Run(Window window)
         at System.Windows.Application.Run()
         at AgentScripting.App.Main()
         at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException: 
         HResult=-2146232000
         LineNumber=1
         LinePosition=1329
         Message=Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content. Line 1, position 1329.
         Source=System.Xml
         StackTrace:
              at System.Xml.XmlReader.ReadElementString()
              at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderUSDOperation.Read4_AgentScript(Boolean isNullable, Boolean checkType)
              at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderUSDOperation.Read5_AgentScripts(Boolean isNullable, Boolean checkType)
              at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderUSDOperation.Read6_AgentBeginScriptList(Boolean isNullable, Boolean checkType)
              at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderUSDOperation.Read11_USDStep(Boolean isNullable, Boolean checkType)
              at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderUSDOperation.Read12_USDSteps(Boolean isNullable, Boolean checkType)
              at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderUSDOperation.Read13_USDOperation(Boolean isNullable, Boolean checkType)
              at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderUSDOperation.Read14_USDOperation()
         InnerException: 
  </code></pre>
  
  <p><strong>-------------------------------EDIT-------------------------------------------</strong></p>
  
  <p><strong>MyOperation.CS</strong> class</p>
  
  <pre><code>public class USDOperation
  {
      [XmlElement(ElementName = "OperationCode")]
      public string OperationCode { get; set; }
  
      [XmlElement(ElementName = "OperationName")]
      public string OperationName { get; set; }
  
      [XmlElement(ElementName = "AuthorizationLevel")]
      public string AuthorizationLevel { get; set; }
  
      [XmlElement(ElementName = "AgentBeginScriptList")]
      public AgentBeginScriptList AgentBeginScriptList { get; set; }
  
      [XmlElement(ElementName = "ReadXML")]
      public string ReadXML { get; set; }
  
      [XmlElement(ElementName = "USDSteps")]
      public USDSteps USDSteps { get; set; }
  
      [XmlElement(ElementName = "AgentEndScriptList")]
      public AgentEndScriptList AgentEndScriptList { get; set; }
  }
  </code></pre>