Read XML as a string from inside XML Document










0














I have an XMLDocument which i am deserializing into object in my C# code. It works fine until i have nodes like below,



 <Name>ABC</Name>
<Message>Invalid Name</Message>


In the above scenario what i get inside my C# object would be Name = ABC and Message = Invalid Name.



But now i have a requirement to read a complete XML node inside XMLDocument. Which is like this,



<ReadXML><fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="customerproduct">
<attribute name="customerproductid" />
<attribute name="name" />
<attribute name="createdon" />
<attribute name="customertype" />
<attribute name="custodian_customerproductid" />
<attribute name="accountnumber" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="productgroup" operator="eq" uiname="Credit Cards" uitype="productgroup" value="12344" />
<condition attribute="customer" operator="eq" uiname="abc xyz" uitype="contact" value="1234" />
</filter>
</entity>
</fetch></ReadXML>


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.



something like this,



string ReadXML = "<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="customerproduct">
<attribute name="customerproductid" />
<attribute name="name" />
<attribute name="createdon" />
<attribute name="customertype" />
<attribute name="custodian_customerproductid" />
<attribute name="accountnumber" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="productgroup" operator="eq" uiname="Credit Cards" uitype="productgroup" value="12344" />
<condition attribute="customer" operator="eq" uiname="abc xyz" uitype="contact" value="1234" />
</filter>
</entity>
</fetch>"


Inside my Code i am doing this,



 XmlDocument doc = new XmlDocument();

doc.Load(@"D:DevelopmentFrameworkTestApplicationXMLScriptXMLScriptXMLFile.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);


And Inside MyOperation Class, i have ReadXML like this,



[XmlElement(ElementName = "ReadXML")]
public string ReadXML get; set;


Whenever i try to read the XML into the string i get the following error,



 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:ProjectsAgentScripting_30102018AgentScriptingAgentScriptingAgentScriptingControl.xaml.cs:line 323
at VeriTouch.CRM.USD.AgentScripting.AgentScriptingControl.StartButton_Click(Object sender, RoutedEventArgs e) in C:ProjectsAgentScripting_30102018AgentScriptingAgentScriptingAgentScriptingControl.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& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& 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& 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:


-------------------------------EDIT-------------------------------------------



MyOperation.CS class



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;










share|improve this question























  • And what is on line 1 at position 1329?
    – John
    Nov 12 at 6:24










  • The ReadXML node.
    – Faran Saleem
    Nov 12 at 6:24










  • could u plz show your MyOperation class?
    – er-shoaib
    Nov 12 at 6:26










  • Please recheck the question i have edited it and added the MyOperation.CS Class. But i don't think that the issue is with MyOperation.CS class because whenever pass a string from XML it is read by my C# variables correctly but whenever i am trying to pass XML and trying to read it as string it gives me an error "Invalid XML"
    – Faran Saleem
    Nov 12 at 6:31










  • yes it gives your error bcoz xml is not suitable for c# classes.
    – er-shoaib
    Nov 12 at 6:34















0














I have an XMLDocument which i am deserializing into object in my C# code. It works fine until i have nodes like below,



 <Name>ABC</Name>
<Message>Invalid Name</Message>


In the above scenario what i get inside my C# object would be Name = ABC and Message = Invalid Name.



But now i have a requirement to read a complete XML node inside XMLDocument. Which is like this,



<ReadXML><fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="customerproduct">
<attribute name="customerproductid" />
<attribute name="name" />
<attribute name="createdon" />
<attribute name="customertype" />
<attribute name="custodian_customerproductid" />
<attribute name="accountnumber" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="productgroup" operator="eq" uiname="Credit Cards" uitype="productgroup" value="12344" />
<condition attribute="customer" operator="eq" uiname="abc xyz" uitype="contact" value="1234" />
</filter>
</entity>
</fetch></ReadXML>


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.



something like this,



string ReadXML = "<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="customerproduct">
<attribute name="customerproductid" />
<attribute name="name" />
<attribute name="createdon" />
<attribute name="customertype" />
<attribute name="custodian_customerproductid" />
<attribute name="accountnumber" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="productgroup" operator="eq" uiname="Credit Cards" uitype="productgroup" value="12344" />
<condition attribute="customer" operator="eq" uiname="abc xyz" uitype="contact" value="1234" />
</filter>
</entity>
</fetch>"


Inside my Code i am doing this,



 XmlDocument doc = new XmlDocument();

doc.Load(@"D:DevelopmentFrameworkTestApplicationXMLScriptXMLScriptXMLFile.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);


And Inside MyOperation Class, i have ReadXML like this,



[XmlElement(ElementName = "ReadXML")]
public string ReadXML get; set;


Whenever i try to read the XML into the string i get the following error,



 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:ProjectsAgentScripting_30102018AgentScriptingAgentScriptingAgentScriptingControl.xaml.cs:line 323
at VeriTouch.CRM.USD.AgentScripting.AgentScriptingControl.StartButton_Click(Object sender, RoutedEventArgs e) in C:ProjectsAgentScripting_30102018AgentScriptingAgentScriptingAgentScriptingControl.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& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& 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& 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:


-------------------------------EDIT-------------------------------------------



MyOperation.CS class



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;










share|improve this question























  • And what is on line 1 at position 1329?
    – John
    Nov 12 at 6:24










  • The ReadXML node.
    – Faran Saleem
    Nov 12 at 6:24










  • could u plz show your MyOperation class?
    – er-shoaib
    Nov 12 at 6:26










  • Please recheck the question i have edited it and added the MyOperation.CS Class. But i don't think that the issue is with MyOperation.CS class because whenever pass a string from XML it is read by my C# variables correctly but whenever i am trying to pass XML and trying to read it as string it gives me an error "Invalid XML"
    – Faran Saleem
    Nov 12 at 6:31










  • yes it gives your error bcoz xml is not suitable for c# classes.
    – er-shoaib
    Nov 12 at 6:34













0












0








0


1





I have an XMLDocument which i am deserializing into object in my C# code. It works fine until i have nodes like below,



 <Name>ABC</Name>
<Message>Invalid Name</Message>


In the above scenario what i get inside my C# object would be Name = ABC and Message = Invalid Name.



But now i have a requirement to read a complete XML node inside XMLDocument. Which is like this,



<ReadXML><fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="customerproduct">
<attribute name="customerproductid" />
<attribute name="name" />
<attribute name="createdon" />
<attribute name="customertype" />
<attribute name="custodian_customerproductid" />
<attribute name="accountnumber" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="productgroup" operator="eq" uiname="Credit Cards" uitype="productgroup" value="12344" />
<condition attribute="customer" operator="eq" uiname="abc xyz" uitype="contact" value="1234" />
</filter>
</entity>
</fetch></ReadXML>


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.



something like this,



string ReadXML = "<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="customerproduct">
<attribute name="customerproductid" />
<attribute name="name" />
<attribute name="createdon" />
<attribute name="customertype" />
<attribute name="custodian_customerproductid" />
<attribute name="accountnumber" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="productgroup" operator="eq" uiname="Credit Cards" uitype="productgroup" value="12344" />
<condition attribute="customer" operator="eq" uiname="abc xyz" uitype="contact" value="1234" />
</filter>
</entity>
</fetch>"


Inside my Code i am doing this,



 XmlDocument doc = new XmlDocument();

doc.Load(@"D:DevelopmentFrameworkTestApplicationXMLScriptXMLScriptXMLFile.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);


And Inside MyOperation Class, i have ReadXML like this,



[XmlElement(ElementName = "ReadXML")]
public string ReadXML get; set;


Whenever i try to read the XML into the string i get the following error,



 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:ProjectsAgentScripting_30102018AgentScriptingAgentScriptingAgentScriptingControl.xaml.cs:line 323
at VeriTouch.CRM.USD.AgentScripting.AgentScriptingControl.StartButton_Click(Object sender, RoutedEventArgs e) in C:ProjectsAgentScripting_30102018AgentScriptingAgentScriptingAgentScriptingControl.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& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& 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& 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:


-------------------------------EDIT-------------------------------------------



MyOperation.CS class



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;










share|improve this question















I have an XMLDocument which i am deserializing into object in my C# code. It works fine until i have nodes like below,



 <Name>ABC</Name>
<Message>Invalid Name</Message>


In the above scenario what i get inside my C# object would be Name = ABC and Message = Invalid Name.



But now i have a requirement to read a complete XML node inside XMLDocument. Which is like this,



<ReadXML><fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="customerproduct">
<attribute name="customerproductid" />
<attribute name="name" />
<attribute name="createdon" />
<attribute name="customertype" />
<attribute name="custodian_customerproductid" />
<attribute name="accountnumber" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="productgroup" operator="eq" uiname="Credit Cards" uitype="productgroup" value="12344" />
<condition attribute="customer" operator="eq" uiname="abc xyz" uitype="contact" value="1234" />
</filter>
</entity>
</fetch></ReadXML>


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.



something like this,



string ReadXML = "<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="customerproduct">
<attribute name="customerproductid" />
<attribute name="name" />
<attribute name="createdon" />
<attribute name="customertype" />
<attribute name="custodian_customerproductid" />
<attribute name="accountnumber" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="productgroup" operator="eq" uiname="Credit Cards" uitype="productgroup" value="12344" />
<condition attribute="customer" operator="eq" uiname="abc xyz" uitype="contact" value="1234" />
</filter>
</entity>
</fetch>"


Inside my Code i am doing this,



 XmlDocument doc = new XmlDocument();

doc.Load(@"D:DevelopmentFrameworkTestApplicationXMLScriptXMLScriptXMLFile.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);


And Inside MyOperation Class, i have ReadXML like this,



[XmlElement(ElementName = "ReadXML")]
public string ReadXML get; set;


Whenever i try to read the XML into the string i get the following error,



 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:ProjectsAgentScripting_30102018AgentScriptingAgentScriptingAgentScriptingControl.xaml.cs:line 323
at VeriTouch.CRM.USD.AgentScripting.AgentScriptingControl.StartButton_Click(Object sender, RoutedEventArgs e) in C:ProjectsAgentScripting_30102018AgentScriptingAgentScriptingAgentScriptingControl.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& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& 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& 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:


-------------------------------EDIT-------------------------------------------



MyOperation.CS class



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;







c# xaml crm unified-service-desk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 6:29

























asked Nov 12 at 6:13









Faran Saleem

1059




1059











  • And what is on line 1 at position 1329?
    – John
    Nov 12 at 6:24










  • The ReadXML node.
    – Faran Saleem
    Nov 12 at 6:24










  • could u plz show your MyOperation class?
    – er-shoaib
    Nov 12 at 6:26










  • Please recheck the question i have edited it and added the MyOperation.CS Class. But i don't think that the issue is with MyOperation.CS class because whenever pass a string from XML it is read by my C# variables correctly but whenever i am trying to pass XML and trying to read it as string it gives me an error "Invalid XML"
    – Faran Saleem
    Nov 12 at 6:31










  • yes it gives your error bcoz xml is not suitable for c# classes.
    – er-shoaib
    Nov 12 at 6:34
















  • And what is on line 1 at position 1329?
    – John
    Nov 12 at 6:24










  • The ReadXML node.
    – Faran Saleem
    Nov 12 at 6:24










  • could u plz show your MyOperation class?
    – er-shoaib
    Nov 12 at 6:26










  • Please recheck the question i have edited it and added the MyOperation.CS Class. But i don't think that the issue is with MyOperation.CS class because whenever pass a string from XML it is read by my C# variables correctly but whenever i am trying to pass XML and trying to read it as string it gives me an error "Invalid XML"
    – Faran Saleem
    Nov 12 at 6:31










  • yes it gives your error bcoz xml is not suitable for c# classes.
    – er-shoaib
    Nov 12 at 6:34















And what is on line 1 at position 1329?
– John
Nov 12 at 6:24




And what is on line 1 at position 1329?
– John
Nov 12 at 6:24












The ReadXML node.
– Faran Saleem
Nov 12 at 6:24




The ReadXML node.
– Faran Saleem
Nov 12 at 6:24












could u plz show your MyOperation class?
– er-shoaib
Nov 12 at 6:26




could u plz show your MyOperation class?
– er-shoaib
Nov 12 at 6:26












Please recheck the question i have edited it and added the MyOperation.CS Class. But i don't think that the issue is with MyOperation.CS class because whenever pass a string from XML it is read by my C# variables correctly but whenever i am trying to pass XML and trying to read it as string it gives me an error "Invalid XML"
– Faran Saleem
Nov 12 at 6:31




Please recheck the question i have edited it and added the MyOperation.CS Class. But i don't think that the issue is with MyOperation.CS class because whenever pass a string from XML it is read by my C# variables correctly but whenever i am trying to pass XML and trying to read it as string it gives me an error "Invalid XML"
– Faran Saleem
Nov 12 at 6:31












yes it gives your error bcoz xml is not suitable for c# classes.
– er-shoaib
Nov 12 at 6:34




yes it gives your error bcoz xml is not suitable for c# classes.
– er-shoaib
Nov 12 at 6:34












3 Answers
3






active

oldest

votes


















1














The problem is that we cannot serialize/deserialize a complex type to string. Instead we can define our own type and implement IXmlSerializable interface to provide our own implementation.



Below I've defined a MyString class to implement the same. In ReadXml method I'm using innerXml. You may use the outerXml as well.



public class MyString : IXmlSerializable

string _xmlString;
public XmlSchema GetSchema()

return null;


public void ReadXml(XmlReader reader)

reader.MoveToContent();

Boolean isEmptyElement = reader.IsEmptyElement;
if (!isEmptyElement)

_xmlString = reader.ReadInnerXml();



public void WriteXml(XmlWriter writer)

writer.WriteString(_xmlString);




The property ReadXML will now be of type MyString instead of string.



[XmlElement(ElementName = "ReadXML")]
public MyString ReadXML get; set;


Ref: I got the idea from the following Stackoverflow question https://stackoverflow.com/a/22106996/643318






share|improve this answer






















  • Thanks Taleeb. It worked
    – Faran Saleem
    Nov 12 at 10:48










  • one more thing. I am trying to convert that into string. But it is not getting converted. Please tell me how can i access the innerXML and then convert that into string and store it inside a variable.
    – Faran Saleem
    Nov 12 at 11:19










  • Can you elaborate. What exactly are you trying to convert?
    – Taleeb
    Nov 13 at 6:43


















0














var xml=File.ReadAllText(@"D:DevelopmentFrameworkTestApplicationXMLScriptXMLScriptXMLFile.xml")





share|improve this answer


















  • 1




    While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – Kurt Van den Branden
    Nov 12 at 14:05


















-1














var xml=File.ReadAllText(@"D:XMLFile.xml");

var xml_l=xml.ToLower();

var ind_1=xml_l.IndexOf("&lt;fetch");
var ind_2=xml_l.IndexOf("&lt;/fetch",ind_1);

var xml_fetch=xml.Substring(ind_1, ind_2 - ind_1+8);





share|improve this answer


















  • 1




    This looks fragile. What if, in future, fetch1 node or or several fetch nodes appear? Or fetch node is renamed and one forget to amend ind_1+8? This introduces extraneous maintenance costs.
    – stop-cran
    Nov 12 at 11:05










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256740%2fread-xml-as-a-string-from-inside-xml-document%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The problem is that we cannot serialize/deserialize a complex type to string. Instead we can define our own type and implement IXmlSerializable interface to provide our own implementation.



Below I've defined a MyString class to implement the same. In ReadXml method I'm using innerXml. You may use the outerXml as well.



public class MyString : IXmlSerializable

string _xmlString;
public XmlSchema GetSchema()

return null;


public void ReadXml(XmlReader reader)

reader.MoveToContent();

Boolean isEmptyElement = reader.IsEmptyElement;
if (!isEmptyElement)

_xmlString = reader.ReadInnerXml();



public void WriteXml(XmlWriter writer)

writer.WriteString(_xmlString);




The property ReadXML will now be of type MyString instead of string.



[XmlElement(ElementName = "ReadXML")]
public MyString ReadXML get; set;


Ref: I got the idea from the following Stackoverflow question https://stackoverflow.com/a/22106996/643318






share|improve this answer






















  • Thanks Taleeb. It worked
    – Faran Saleem
    Nov 12 at 10:48










  • one more thing. I am trying to convert that into string. But it is not getting converted. Please tell me how can i access the innerXML and then convert that into string and store it inside a variable.
    – Faran Saleem
    Nov 12 at 11:19










  • Can you elaborate. What exactly are you trying to convert?
    – Taleeb
    Nov 13 at 6:43















1














The problem is that we cannot serialize/deserialize a complex type to string. Instead we can define our own type and implement IXmlSerializable interface to provide our own implementation.



Below I've defined a MyString class to implement the same. In ReadXml method I'm using innerXml. You may use the outerXml as well.



public class MyString : IXmlSerializable

string _xmlString;
public XmlSchema GetSchema()

return null;


public void ReadXml(XmlReader reader)

reader.MoveToContent();

Boolean isEmptyElement = reader.IsEmptyElement;
if (!isEmptyElement)

_xmlString = reader.ReadInnerXml();



public void WriteXml(XmlWriter writer)

writer.WriteString(_xmlString);




The property ReadXML will now be of type MyString instead of string.



[XmlElement(ElementName = "ReadXML")]
public MyString ReadXML get; set;


Ref: I got the idea from the following Stackoverflow question https://stackoverflow.com/a/22106996/643318






share|improve this answer






















  • Thanks Taleeb. It worked
    – Faran Saleem
    Nov 12 at 10:48










  • one more thing. I am trying to convert that into string. But it is not getting converted. Please tell me how can i access the innerXML and then convert that into string and store it inside a variable.
    – Faran Saleem
    Nov 12 at 11:19










  • Can you elaborate. What exactly are you trying to convert?
    – Taleeb
    Nov 13 at 6:43













1












1








1






The problem is that we cannot serialize/deserialize a complex type to string. Instead we can define our own type and implement IXmlSerializable interface to provide our own implementation.



Below I've defined a MyString class to implement the same. In ReadXml method I'm using innerXml. You may use the outerXml as well.



public class MyString : IXmlSerializable

string _xmlString;
public XmlSchema GetSchema()

return null;


public void ReadXml(XmlReader reader)

reader.MoveToContent();

Boolean isEmptyElement = reader.IsEmptyElement;
if (!isEmptyElement)

_xmlString = reader.ReadInnerXml();



public void WriteXml(XmlWriter writer)

writer.WriteString(_xmlString);




The property ReadXML will now be of type MyString instead of string.



[XmlElement(ElementName = "ReadXML")]
public MyString ReadXML get; set;


Ref: I got the idea from the following Stackoverflow question https://stackoverflow.com/a/22106996/643318






share|improve this answer














The problem is that we cannot serialize/deserialize a complex type to string. Instead we can define our own type and implement IXmlSerializable interface to provide our own implementation.



Below I've defined a MyString class to implement the same. In ReadXml method I'm using innerXml. You may use the outerXml as well.



public class MyString : IXmlSerializable

string _xmlString;
public XmlSchema GetSchema()

return null;


public void ReadXml(XmlReader reader)

reader.MoveToContent();

Boolean isEmptyElement = reader.IsEmptyElement;
if (!isEmptyElement)

_xmlString = reader.ReadInnerXml();



public void WriteXml(XmlWriter writer)

writer.WriteString(_xmlString);




The property ReadXML will now be of type MyString instead of string.



[XmlElement(ElementName = "ReadXML")]
public MyString ReadXML get; set;


Ref: I got the idea from the following Stackoverflow question https://stackoverflow.com/a/22106996/643318







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 7:52

























answered Nov 12 at 7:46









Taleeb

1,4031420




1,4031420











  • Thanks Taleeb. It worked
    – Faran Saleem
    Nov 12 at 10:48










  • one more thing. I am trying to convert that into string. But it is not getting converted. Please tell me how can i access the innerXML and then convert that into string and store it inside a variable.
    – Faran Saleem
    Nov 12 at 11:19










  • Can you elaborate. What exactly are you trying to convert?
    – Taleeb
    Nov 13 at 6:43
















  • Thanks Taleeb. It worked
    – Faran Saleem
    Nov 12 at 10:48










  • one more thing. I am trying to convert that into string. But it is not getting converted. Please tell me how can i access the innerXML and then convert that into string and store it inside a variable.
    – Faran Saleem
    Nov 12 at 11:19










  • Can you elaborate. What exactly are you trying to convert?
    – Taleeb
    Nov 13 at 6:43















Thanks Taleeb. It worked
– Faran Saleem
Nov 12 at 10:48




Thanks Taleeb. It worked
– Faran Saleem
Nov 12 at 10:48












one more thing. I am trying to convert that into string. But it is not getting converted. Please tell me how can i access the innerXML and then convert that into string and store it inside a variable.
– Faran Saleem
Nov 12 at 11:19




one more thing. I am trying to convert that into string. But it is not getting converted. Please tell me how can i access the innerXML and then convert that into string and store it inside a variable.
– Faran Saleem
Nov 12 at 11:19












Can you elaborate. What exactly are you trying to convert?
– Taleeb
Nov 13 at 6:43




Can you elaborate. What exactly are you trying to convert?
– Taleeb
Nov 13 at 6:43













0














var xml=File.ReadAllText(@"D:DevelopmentFrameworkTestApplicationXMLScriptXMLScriptXMLFile.xml")





share|improve this answer


















  • 1




    While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – Kurt Van den Branden
    Nov 12 at 14:05















0














var xml=File.ReadAllText(@"D:DevelopmentFrameworkTestApplicationXMLScriptXMLScriptXMLFile.xml")





share|improve this answer


















  • 1




    While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – Kurt Van den Branden
    Nov 12 at 14:05













0












0








0






var xml=File.ReadAllText(@"D:DevelopmentFrameworkTestApplicationXMLScriptXMLScriptXMLFile.xml")





share|improve this answer














var xml=File.ReadAllText(@"D:DevelopmentFrameworkTestApplicationXMLScriptXMLScriptXMLFile.xml")






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 10:23









er-shoaib

4,8342516




4,8342516










answered Nov 12 at 7:41









Muhammad Liaquat

62954




62954







  • 1




    While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – Kurt Van den Branden
    Nov 12 at 14:05












  • 1




    While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – Kurt Van den Branden
    Nov 12 at 14:05







1




1




While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Kurt Van den Branden
Nov 12 at 14:05




While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Kurt Van den Branden
Nov 12 at 14:05











-1














var xml=File.ReadAllText(@"D:XMLFile.xml");

var xml_l=xml.ToLower();

var ind_1=xml_l.IndexOf("&lt;fetch");
var ind_2=xml_l.IndexOf("&lt;/fetch",ind_1);

var xml_fetch=xml.Substring(ind_1, ind_2 - ind_1+8);





share|improve this answer


















  • 1




    This looks fragile. What if, in future, fetch1 node or or several fetch nodes appear? Or fetch node is renamed and one forget to amend ind_1+8? This introduces extraneous maintenance costs.
    – stop-cran
    Nov 12 at 11:05















-1














var xml=File.ReadAllText(@"D:XMLFile.xml");

var xml_l=xml.ToLower();

var ind_1=xml_l.IndexOf("&lt;fetch");
var ind_2=xml_l.IndexOf("&lt;/fetch",ind_1);

var xml_fetch=xml.Substring(ind_1, ind_2 - ind_1+8);





share|improve this answer


















  • 1




    This looks fragile. What if, in future, fetch1 node or or several fetch nodes appear? Or fetch node is renamed and one forget to amend ind_1+8? This introduces extraneous maintenance costs.
    – stop-cran
    Nov 12 at 11:05













-1












-1








-1






var xml=File.ReadAllText(@"D:XMLFile.xml");

var xml_l=xml.ToLower();

var ind_1=xml_l.IndexOf("&lt;fetch");
var ind_2=xml_l.IndexOf("&lt;/fetch",ind_1);

var xml_fetch=xml.Substring(ind_1, ind_2 - ind_1+8);





share|improve this answer














var xml=File.ReadAllText(@"D:XMLFile.xml");

var xml_l=xml.ToLower();

var ind_1=xml_l.IndexOf("&lt;fetch");
var ind_2=xml_l.IndexOf("&lt;/fetch",ind_1);

var xml_fetch=xml.Substring(ind_1, ind_2 - ind_1+8);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 10:23









er-shoaib

4,8342516




4,8342516










answered Nov 12 at 7:50









Muhammad Liaquat

62954




62954







  • 1




    This looks fragile. What if, in future, fetch1 node or or several fetch nodes appear? Or fetch node is renamed and one forget to amend ind_1+8? This introduces extraneous maintenance costs.
    – stop-cran
    Nov 12 at 11:05












  • 1




    This looks fragile. What if, in future, fetch1 node or or several fetch nodes appear? Or fetch node is renamed and one forget to amend ind_1+8? This introduces extraneous maintenance costs.
    – stop-cran
    Nov 12 at 11:05







1




1




This looks fragile. What if, in future, fetch1 node or or several fetch nodes appear? Or fetch node is renamed and one forget to amend ind_1+8? This introduces extraneous maintenance costs.
– stop-cran
Nov 12 at 11:05




This looks fragile. What if, in future, fetch1 node or or several fetch nodes appear? Or fetch node is renamed and one forget to amend ind_1+8? This introduces extraneous maintenance costs.
– stop-cran
Nov 12 at 11:05

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256740%2fread-xml-as-a-string-from-inside-xml-document%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党