question

PARTHDESAI-2292 avatar image
0 Votes"
PARTHDESAI-2292 asked ·

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

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();
}


dotnet-csharp
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@PARTHDESAI-2292,

I have two xml's one is source and other one is destination.

In such case, why do you need serialization/deserialization?
It is better to use XSLT transformation for such scenario.



0 Votes 0 ·

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered ·

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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.