List of property that don't exists in XML before deserialize

PARTH DESAI 61 Reputation points
2021-03-14T10:29:35.62+00:00

Hello,

I have two xml's one is source and other one is destination. I want to update all the properties after deserialization of xml files from destination to source only If node with property name do exists. If node is not present than instead of default value I want the value to be get updated from source.
Below is my code of deserialization

XmlSerializer deserializer = new XmlSerializer(typeof(MyClass));
using (TextReader reader = new StreamReader(filename))
{
object obj = deserializer.Deserialize(reader);
deserializer = null;
reader.Close();
reader.Dispose();
}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,279 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-03-15T09:54:14.117+00:00

    Try to use code like this to modify the value in xml:

            public static void Main (string[] args)  
            {  
                XDocument xdoc = XDocument.Load(@"file.xml");  
                var element = xdoc.Root.Elements("MyXmlElement").Single();  
                element.Value = "new value";  
                xdoc.Save(@"file.xml");  
            }  
    

    It seems that there is no particularly good way to determine whether certain attributes exist except for adding some if statements.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments