如何将XML转换为C#对象

时间:2015-08-31 09:32:39

标签: c# xml deserialization

是否有基于XML创建类结构的工具或者反序列化XML文档的简便方法?我的XML中的标签是标准的,但我不知道在C#中使用这个文档的最佳方式。

XML看起来像这样:

   <?xml version="1.0" encoding="utf-8"?>
<shapes>
  <FunctionalBlocks>
    <FunctionalBlock uid="{43FA5CE2-3A26-4276-AE30-54DFF4D28E36}" name="1FTT001">
      <props>
        <prop name="Typical or Mode" />
        <prop name="Function Template Type">MA</prop>
        <prop name="Node">01</prop>
        <prop name="System (Tag)">1</prop>
        <prop name="Instrumentation Identification (Tag)">FTT</prop>
        <prop name="Sequence Number (Tag)">001</prop>
        <prop name="Comment">Produced water</prop>
        <prop name="SAS System">C</prop>
        <prop name="EffectList" />
        <prop name="Shape Type">FunctionBlock</prop>
      </props>
      <pos left="2.283464" top="17.244094" right="3.228346" bottom="16.614173" />
      <connects>
        <In>
          <connect input="X" uid="{41872BA8-4AE2-40DA-AE93-3A4BCBE24443}" />
        </In>
        <Out>
          <connect output="AHH" uid="{C439DFA1-1A80-414E-9CF5-FBD7D3A4FA77}">.X1</connect>
        </Out>
        <Undirected />
      </connects>
    </FunctionalBlock>
    <FunctionalBlock uid="{AB65A176-9C8A-4F63-901E-715B4709FFE2}" name="1UV001">
      <props>
        <prop name="Typical or Mode" />
        <prop name="Function Template Type">SBE</prop>
        <prop name="Node">01</prop>
        <prop name="System (Tag)">1</prop>
        <prop name="Instrumentation Identification (Tag)">UV</prop>
        <prop name="Sequence Number (Tag)">001</prop>
        <prop name="Comment" />
        <prop name="SAS System">C</prop>
        <prop name="EffectList" />
        <prop name="Shape Type">FunctionBlock</prop>
      </props>
      <pos left="6.456692" top="16.929133" right="7.401574" bottom="16.299212" />
      <connects>
        <In>
          <connect input="FSL" uid="{FD6A0B2D-729B-474C-A5B1-A49BB1CC481D}">O</connect>
        </In>
        <Out>
          <connect output="BA" uid="{57B4BE80-66E7-4E19-AEA1-AC8EBA01B195}">&amp;.X2</connect>
          <connect output="Y" uid="{57B4BE80-66E7-4E19-AEA1-AC8EBA01B195}">&amp;.X1</connect>
        </Out>
        <Undirected />
      </connects>
    </FunctionalBlock>
    <FunctionalBlock uid="{F720A6D5-3A87-4BF1-8D0D-43E6481481A9}" name="1FT002">
      <props>
        <prop name="Typical or Mode" />
        <prop name="Function Template Type">MA</prop>
        <prop name="Node">01</prop>
        <prop name="System (Tag)">1</prop>
        <prop name="Instrumentation Identification (Tag)">FT</prop>
        <prop name="Sequence Number (Tag)">002</prop>
        <prop name="Comment">Produced water</prop>
        <prop name="SAS System">C</prop>
        <prop name="EffectList" />
        <prop name="Shape Type">FunctionBlock</prop>
      </props>
      <pos left="2.913385" top="16.141732" right="3.858267" bottom="15.511811" />
      <connects>
        <In>
          <connect input="X" uid="{568DF49F-C484-4860-86FC-E373C5FBE5E5}" />
        </In>
        <Out>
          <connect output="AHH" uid="{FD6A0B2D-729B-474C-A5B1-A49BB1CC481D}">O.X2</connect>
        </Out>
        <Undirected />
      </connects>
    </FunctionalBlock>
  </FunctionalBlocks>
  <SoftwareFunctions>
    <SoftwareFunction uid="{FD6A0B2D-729B-474C-A5B1-A49BB1CC481D}" name="Software function.54">
      <props>
        <prop name="Function Type">O</prop>
        <prop name="Node" />
        <prop name="SAS System">C</prop>
        <prop name="Shape Type">SoftwareFunction</prop>
      </props>
      <pos left="4.803149" top="16.377952" right="4.960629" bottom="16.220472" />
      <connects>
        <In>
          <connect input="" uid="{F720A6D5-3A87-4BF1-8D0D-43E6481481A9}">1FT002.AHH</connect>
          <connect input="" uid="{C439DFA1-1A80-414E-9CF5-FBD7D3A4FA77}" />
        </In>
        <Out>
          <connect output="" uid="{AB65A176-9C8A-4F63-901E-715B4709FFE2}">1UV001.FSL</connect>
        </Out>
        <Undirected />
      </connects>
    </SoftwareFunction>
    <SoftwareFunction uid="{57B4BE80-66E7-4E19-AEA1-AC8EBA01B195}" name="Software function.51">
      <props>
        <prop name="Function Type">&amp;</prop>
        <prop name="Node" />
        <prop name="SAS System">C</prop>
        <prop name="Shape Type">SoftwareFunction</prop>
      </props>
      <pos left="8.031496" top="16.377952" right="8.188976" bottom="16.220472" />
      <connects>
        <In>
          <connect input="" uid="{AB65A176-9C8A-4F63-901E-715B4709FFE2}">1UV001.BA</connect>
          <connect input="" uid="{AB65A176-9C8A-4F63-901E-715B4709FFE2}">1UV001.Y</connect>
        </In>
        <Out />
        <Undirected />
      </connects>
    </SoftwareFunction>
  </SoftwareFunctions>
  <Uncategorizeds>
    <Uncategorized uid="{9537F074-8384-4FBE-B24E-56260F192AF8}" name="General Signal.53">
      <props>
        <prop name="Input Terminal">X1</prop>
        <prop name="Output Terminal">AHH</prop>
        <prop name="Shape Type">GeneralSignal</prop>
        <prop name="SAS System">C</prop>
        <prop name="Node">01</prop>
      </props>
      <pos left="3.228346" top="16.978346" right="4.094488" bottom="16.639763" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{1D0469E0-F58F-44C4-9F26-B262052B2124}" name="General Signal.32">
      <props>
        <prop name="Input Terminal">X2</prop>
        <prop name="Output Terminal">AHH</prop>
        <prop name="Shape Type">GeneralSignal</prop>
        <prop name="SAS System">C</prop>
        <prop name="Node">01</prop>
      </props>
      <pos left="3.858267" top="16.220472" right="4.881889" bottom="15.905511" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{77599DF1-887F-4697-A7F9-7ACD90769333}" name="General Signal.35">
      <props>
        <prop name="Input Terminal">FSL</prop>
        <prop name="Output Terminal" />
        <prop name="Shape Type">GeneralSignal</prop>
        <prop name="SAS System">C</prop>
        <prop name="Node" />
      </props>
      <pos left="4.960629" top="16.663385" right="6.456692" bottom="16.299212" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{568DF49F-C484-4860-86FC-E373C5FBE5E5}" name="Transmitter.55">
      <props />
      <pos left="2.007874" top="15.787401" right="2.086614" bottom="15.551181" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{59D08AFC-056F-48A8-BD02-00FCA344CE0A}" name="General Signal.41">
      <props>
        <prop name="Input Terminal">X</prop>
        <prop name="Output Terminal" />
        <prop name="Shape Type">GeneralSignal</prop>
        <prop name="SAS System">C</prop>
        <prop name="Node">01</prop>
      </props>
      <pos left="2.086614" top="15.875984" right="2.913385" bottom="15.649606" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{C439DFA1-1A80-414E-9CF5-FBD7D3A4FA77}" name="Timer">
      <props>
        <prop name="Edge">Rising</prop>
        <prop name="Delay">2 s</prop>
        <prop name="Node" />
        <prop name="SAS System">C</prop>
        <prop name="Shape Type">Pulse</prop>
      </props>
      <pos left="4.094488" top="16.777559" right="4.386333" bottom="16.450787" />
      <connects>
        <In>
          <connect input="" uid="{43FA5CE2-3A26-4276-AE30-54DFF4D28E36}">1FTT001.AHH</connect>
        </In>
        <Out>
          <connect output="" uid="{FD6A0B2D-729B-474C-A5B1-A49BB1CC481D}">O.X1</connect>
        </Out>
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{39E51911-46FC-4873-AE6D-039877FB29DB}" name="General Signal.48">
      <props>
        <prop name="Input Terminal">X1</prop>
        <prop name="Output Terminal" />
        <prop name="Shape Type">GeneralSignal</prop>
        <prop name="SAS System">C</prop>
        <prop name="Node" />
      </props>
      <pos left="4.251968" top="16.767716" right="4.881889" bottom="16.377952" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{ACB6FB09-3CC4-418D-A398-400E4E54F8B3}" name="Heater">
      <props />
      <pos left="7.716535" top="15.866141" right="8.637795" bottom="15.511811" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{722E33C2-98BC-44E4-8BA5-19BA757889FE}" name="General Signal.58">
      <props>
        <prop name="Input Terminal">X1</prop>
        <prop name="Output Terminal">Y</prop>
        <prop name="Shape Type">GeneralSignal</prop>
        <prop name="SAS System">C</prop>
        <prop name="Node">01</prop>
      </props>
      <pos left="7.401574" top="16.663385" right="8.355797" bottom="16.358267" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{B4676D69-1D21-42FA-8B0D-12ABE6563DDC}" name="General Signal.61">
      <props>
        <prop name="Input Terminal">X2</prop>
        <prop name="Output Terminal">BA</prop>
        <prop name="Shape Type">GeneralSignal</prop>
        <prop name="SAS System">C</prop>
        <prop name="Node">01</prop>
      </props>
      <pos left="7.401574" top="16.505905" right="8.031496" bottom="16.240157" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{B33BA753-7E31-4D28-8991-6F79445BF0DC}" name="General Signal.64">
      <props>
        <prop name="Input Terminal" />
        <prop name="Output Terminal">Y</prop>
        <prop name="Shape Type">GeneralSignal</prop>
        <prop name="SAS System">C</prop>
        <prop name="Node" />
      </props>
      <pos left="7.811023" top="16.427165" right="8.372743" bottom="15.511811" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{41872BA8-4AE2-40DA-AE93-3A4BCBE24443}" name="Transmitter.67">
      <props />
      <pos left="1.889763" top="16.929133" right="1.968503" bottom="16.692913" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
    <Uncategorized uid="{C699BDD4-6A8A-473F-9A50-4FFC0E23E0D1}" name="General Signal.70">
      <props>
        <prop name="Input Terminal">X</prop>
        <prop name="Output Terminal" />
        <prop name="Shape Type">GeneralSignal</prop>
        <prop name="SAS System">C</prop>
        <prop name="Node">01</prop>
      </props>
      <pos left="1.968503" top="17.017716" right="2.283464" bottom="16.771653" />
      <connects>
        <In />
        <Out />
        <Undirected />
      </connects>
    </Uncategorized>
  </Uncategorizeds>
</shapes>

1 个答案:

答案 0 :(得分:0)

试试这个希望这对你来说是

XmlDocument xmlDoc =new XmlDocument();   //Represents an XML document, 
                // Initializes a new instance of the XmlDocument class.          
      XmlSerializer xmlSerializer = new XmlSerializer(YourClassObject.GetType());            
    // Creates a stream whose backing store is memory. 
       using (MemoryStream xmlStream =new MemoryStream())
       { 
        xmlSerializer.Serialize(xmlStream, YourClassObject);
        xmlStream.Position = 0;
        //Loads the XML document from the specified string.
        xmlDoc.Load(xmlStream); 
        return xmlDoc.InnerXml;
       }
}

此方法将返回包含XML的字符串。

使用CreateXML方法。

string strView =CreateXML(YourClassObject);

创建一个将XML转换为对象的方法。

public Object CreateObject(string XMLString,Object YourClassObject){            
    XmlSerializer oXmlSerializer =new XmlSerializer(YourClassObject.GetType()); 
    //The StringReader will be the stream holder for the existing XML file 
    YourClassObject = oXmlSerializer.Deserialize(new StringReader(XMLString)); 
 //initially deserialized, the data is represented by an object without a defined type 
 return YourClassObject;
 }
} 

使用CreateObject方法:

Employee objemp = newEmployee();   

objemp = (Employee)CreateObject(txtXML.Text, objemp)