none
XDocument - Same declaration RRS feed

  • Question

  • Hello,
    I must create 20 XML Messages.
    The header is always the same.

    Is by reference. Is it possible by value? Or what can I do?

    XDocument localXDoc = XDoc;
    XElement localRoot = Root;

    If I change inside localXDoc also XDoc is changed.
    With best regards Markus
    public class Requests
    {
    	private XNamespace XSI = "http://www.w3.org/2001/XMLSchema-instance";
    	private XDocument XDoc;
    	private XElement Root;
    	private XDeclaration Dec = new XDeclaration("1.0", "ISO-8859-1", "");
    
    	public Requests()
    	{
    		Root = new XElement("control",
    		new XAttribute(XSI + "noNamespaceSchemaLocation", "control-1.0.xsd"),
    		new XAttribute("locale", "english"),
    		new XAttribute(XNamespace.Xmlns + "xsi", XSI));
    
    		XDoc = new XDocument(Dec, Root);
    	}
    
    
    	public string GetOrderRequestTelegram(string requestID, string order)
    	{
    		string ret = "";
    
    		//XDoc = new XDocument(
    		//    new XDeclaration("1.0", "ISO-8859-1", ""),
    		//    Root);
    
    		XDocument localXDoc = XDoc;
    		XElement localRoot = Root;
    		
    	public string SecondTelegram(string requestID)
    	{
    		string ret = "";
    
    		XDocument localXDoc = XDoc;
    		XElement localRoot = Root

    Friday, February 9, 2018 5:33 PM

Answers

  • Hello Stefan, Tim,
    I do it.
    Well, I think this way is right.
    Root use all 20 messages. So far, I can create as class member.
    XElement localRoot = new XElement(Root);
    XDocument localXDoc = new XDocument(Dec, localRoot);
    //XDoc = new XDocument();
    //XDoc.Add(localRoot);

    With best regards Markus
    public class Requests
        {
            private XNamespace XSI = "http://www.w3.org/2001/XMLSchema-instance";
            private XDocument XDoc;
            private XElement Root;
            private XDeclaration Dec = new XDeclaration("1.0", "ISO-8859-1", "");
    
            public ZVEIRequests()
            {
                Root = new XElement("control",
                new XAttribute(XSI + "noNamespaceSchemaLocation", "control-1.0.xsd"),
                new XAttribute("locale", "english"),
                new XAttribute(XNamespace.Xmlns + "xsi", XSI));
    
                XDoc = new XDocument(Dec, Root);
            }
    
      public string GetOrderRequestTelegram(string requestID, string order)
            {
                string ret = "";
                XDocument localXDoc = XDoc;
                XElement localRoot = Root;
    
      public string GetOrderMaterialTelegram(string requestID, string order)
            {
                string ret = "";
                XDocument localXDoc = XDoc;
                XElement localRoot = Root;
    
    • Marked as answer by Markus Freitag Tuesday, February 13, 2018 5:04 PM
    Monday, February 12, 2018 5:32 PM

All replies

  • Hello Markus,

    >>Is by reference. Is it possible by value? Or what can I do? If I change inside localXDoc also XDoc is changed.

    What do you want to achieve? I'm not sure the "20 XML Messages" meaning. Do you want to output 20 xml files that each contains XDocument info? If so, you need to make a deep copy rather than assigned reference. Try to use this constructor to make a deep copy of an XDocument.

     XDocument localXDoc = new XDocument( XDoc);

    This constructor traverses all nodes and attributes in the document specified in the other parameter, and creates copies of all nodes as it assembles the newly initialized XDocument.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, February 11, 2018 11:11 AM
    Moderator
  • Hi Fei Hu,

    public string GetOrderRequestTelegram(string requestID, string order)
    	{
    		string ret = "";
    		XDocument localXDoc = XDoc;
    		XElement localRoot = Root;
    
    • GetMaterialRequestTelegram
    • GetStatusRequestTelegram
    • GetProductGroupRequestTelegram
    • GetResultGroupRequestTelegram

    ...

    I have to generate about 20 queries, which then provide an answer.

    • The head of these requests is always the same.
      - XDocument localXDoc = XDoc;
      - XElement localRoot = Root;
      If I do it that way, change it locally, then the members of the class have the same.

    Why?
    How is the better way?

    My idea was to create the header and just add the own element for the 20 telegrams.

    I hope you have recognized my goal and understood.

    With best regards Markus

    Sunday, February 11, 2018 11:31 AM
  • The questions are:

    Why do you need to generate them? And what kind of architecture do you use?

    Cause I would normally consider using standard .NET XML serialization in the first place.

    Sunday, February 11, 2018 11:44 AM
  • What are you trying to save here?  The CPU time involved is trivial; there's no point in trying to optimize this for performance.  If you're trying to avoid typing (which IS a good goal), you can certainly create a function that does all of the common setup for your request, and call that from your individual Get functions.

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Monday, February 12, 2018 6:57 AM
  • Hello Stefan, Tim,
    I do it.
    Well, I think this way is right.
    Root use all 20 messages. So far, I can create as class member.
    XElement localRoot = new XElement(Root);
    XDocument localXDoc = new XDocument(Dec, localRoot);
    //XDoc = new XDocument();
    //XDoc.Add(localRoot);

    With best regards Markus
    public class Requests
        {
            private XNamespace XSI = "http://www.w3.org/2001/XMLSchema-instance";
            private XDocument XDoc;
            private XElement Root;
            private XDeclaration Dec = new XDeclaration("1.0", "ISO-8859-1", "");
    
            public ZVEIRequests()
            {
                Root = new XElement("control",
                new XAttribute(XSI + "noNamespaceSchemaLocation", "control-1.0.xsd"),
                new XAttribute("locale", "english"),
                new XAttribute(XNamespace.Xmlns + "xsi", XSI));
    
                XDoc = new XDocument(Dec, Root);
            }
    
      public string GetOrderRequestTelegram(string requestID, string order)
            {
                string ret = "";
                XDocument localXDoc = XDoc;
                XElement localRoot = Root;
    
      public string GetOrderMaterialTelegram(string requestID, string order)
            {
                string ret = "";
                XDocument localXDoc = XDoc;
                XElement localRoot = Root;
    
    • Marked as answer by Markus Freitag Tuesday, February 13, 2018 5:04 PM
    Monday, February 12, 2018 5:32 PM
  • Just a comment:

    Well, I think this way is right.

    Root use all 20 messages. So far, I can create as class member.

    I slightly disagree. Without knowing your internals it's hard to tell, but it has a code smell. It looks like you should take a look at what you're doing from an architectural perspective. Cause GetOrderRequestTelegram and GetOrderMaterialTelegram indicate some design patterns: strategy or template. And a Request class on its own is also semantically not ideal.

    And last, but not least: Request also indicates that you're working on a kind of service (web, rest, soap). Here is then the question: why doing it manually?

    Thursday, February 15, 2018 10:00 AM