XML : How do I preserve namespace and attributes when reading an XML fragment generated by DataContractSerializer?

I have a class and I used DataContractSerializer to create XML that looks like this

  <processorSettings xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/myProcessor">    <DateFormats xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />    <PersistentKey i:nil="true" />    <Targets>       <Target>         <locationId>1</locationId>         <targetType>identifier</targetType>       </Target>    </Targets>  </processorSettings>    

Well this class actually represents the setting to a plug in so it needs to be placed in the main application configuration XML file like this

  <mainConfig objectId="1" type="CSV">    <mainSettings>  ....  </mainSettings>       <processors>        <processor processorId="123" processorType="myProcessor">              <otherparameters>              ....                  </otherparameters>                <processorSettings xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/myProcessor">              <DateFormats xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />              <PersistentKey i:nil="true" />              <Targets>              <Target>              <locationId>1</locationId>              <targetType>identifier</targetType>              </Target>              </Targets>              </processorSettings>          </processor>      </processors>        <otherStuff>      ....      </otherStuff>    </mainConfig>    

I use IXmlSerializable inteface to write my own serialization for the main configuration. In it I am using XElement to represent the root. Then I use Linq to Xml to navigate to get my data.

  XElement root = XNode.ReadFrom(reader) as XElement;    

When I get to the node for the processesorSettings I tried using this with no namespace which return NULL

  XElement elem = node.Element("processorSettings");    

Then I tried this

  XElement elem = node.Element(node.GetDefaultNamespace() + "processorSettings");    

Which partially worked, it returns the XML but it is missing all the namespace information and the attributes for data types.

I don't know ahead of time what plug in my configuration will have so I cannot know ahead of time what the name space will be.

And I need to preserve the namespace and data type attributes because after I read the XML string the next step I need to do is deserialize back to the class object.

Tiada ulasan:

Catat Ulasan