从 XmlSerializer 中删除节点

Remove Node from XmlSerializer

我想将 WCF 请求作为文件保存到目录中,但是当从 XmlSerializer 创建文件 (xml) 时,它会向 XML 文件添加一个节点,而我没有这样做想。已调用 ("ProjectInRequest")。我不想在创建的输出文件中包含此节点。 我怎样才能最有效地删除它?

我已尝试将整个文档加载到应该创建文件的 XmlDocument 中,但是在返回响应之前不会创建文件。因此整个服务失败。

namespace SymbrioProjectIn
{

    public partial class SymbrioProjectIn_v1
    {
        public ProjectInResponse GetSymbrioProjectInRequest(ProjectInRequest request)
        {

            if (request == null || request.SymbrioXMLProjectIn.Project == null && request.SymbrioXMLProjectIn.Routing == null )
            {
                throw new ApplicationException("Request is null: You need to send info in request.");
            }
            else
            {
                try
                {
                    //All we want to do is take the Request that SAP sends and store it to a file catalog.
                    //We can after that use BizTalk to move the file too another sftp catalog for Symbrio
                    string path = AppDomain.CurrentDomain.BaseDirectory;
                    String dir = Path.GetDirectoryName(path);
                    dir += "\ProjectIn_Data";
                    string filename = dir + "\ProjectIn.xml";
                    if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir); // inside the if statement   

                    //XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    //Add an empty namespace and empty value
                    //ns.(string.Empty, string.Empty);
                    //XElement ProjectInRequest = new XElement("ProjectInRequest");
                    //ProjectInRequest.RemoveAll();

                    /*XmlAttributes ignore = new XmlAttributes() { XmlIgnore = true };
                    XmlAttributeOverrides overrides = new XmlAttributeOverrides();
                    overrides.Add(typeof(ProjectInRequest), "ProjectInRequest", ignore);*/

                    XmlSerializer ser = new XmlSerializer(request.GetType(), new XmlRootAttribute("ProjectInRequest").ElementName); //Tried som code from internet  not sure what it does               
                    FileStream fs = File.Open(
                            filename,
                            FileMode.OpenOrCreate,
                            FileAccess.Write,
                            FileShare.ReadWrite);
                    ser.Serialize(fs, request);                    
                    var response = new SymbrioProjectIn.ProjectInResponse{
                        SymbrioXMLProjectIn = request.SymbrioXMLProjectIn                        
                    };

                   //Looks like xmlSerialiser creates some type of namespace which I do not want.
                    //Create our own namespaces for the output
                    XmlDocument XDoc = new XmlDocument();
                    XDoc.Load(@"D:\ServiceArea\SymbrioProjectIn\ProjectIn_Data\ProjectIn.xml");
                    XmlNodeList RemoveNodeProjectInRequest = XDoc.GetElementsByTagName("ProjectInRequest");
                    foreach (XmlNode Node in RemoveNodeProjectInRequest)
                    {
                        Node.ParentNode.RemoveChild(Node);
                    }
                    XDoc.Save(@"D:\ServiceArea\SymbrioProjectIn\ProjectIn_Data\ProjectIn.xml"); 

                    return response;
                }
                catch (Exception ex)
                {
                    //throw new Exception(ex.Message);
                    throw new ApplicationException(ex.Message);

                } 
            }

        } 

    }
}

创建的文件如下所示

<?xml version="1.0"?>
<ProjectInRequest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="ProjectInRequest">
  <SymbrioXMLProjectIn>
    <Routing SourceValue="?" SourceType="?" DestinationValue="?" DestinationType="?" xmlns="http://www.symbrio.com/schemas/symbrioprojectin20133" />
    <Project xmlns="http://www.symbrio.com/schemas/symbrioprojectin20133">
      <CompanyCode>?</CompanyCode>
      <DivisionCode>?</DivisionCode>
      <MainProjectNo>?</MainProjectNo>
      <ProjectNo>?</ProjectNo>
      <ProjectName>?</ProjectName>
      <ProjectType>?</ProjectType>
      <PLCode>?</PLCode>
      <EmploymentNo>?</EmploymentNo>
      <Active>?</Active>
      <ActiveStart>?</ActiveStart>
      <ActiveEnd>?</ActiveEnd>
      <DeliveryAddress>?</DeliveryAddress>
      <DeliveryPostalCode>?</DeliveryPostalCode>
      <DeliveryCity>?</DeliveryCity>
      <DeliveryAddressNoteText>?</DeliveryAddressNoteText>
      <AccountRef1>?</AccountRef1>
      <AccountRef2>?</AccountRef2>
      <AccountRef3>?</AccountRef3>
      <AccountRef4>?</AccountRef4>
      <RegisteredDate>?</RegisteredDate>
      <Closed>?</Closed>
    </Project>
  </SymbrioXMLProjectIn>
</ProjectInRequest>

我要的就是这个,

<?xml version="1.0" encoding="utf-8"?>
<SymbrioXMLProjectIn>
  <Routing SourceValue="str1234" SourceType="str1234" DestinationValue="str1234" DestinationType="str1234" />
  <Project>
    <CompanyCode>str1234</CompanyCode>
    <DivisionCode>str1234</DivisionCode>
    <MainProjectNo>str1234</MainProjectNo>
    <ProjectNo>str1234</ProjectNo>
    <ProjectName>str1234</ProjectName>
    <ProjectType>str1234</ProjectType>
    <PLCode>str1234</PLCode>
    <EmploymentNo>str1234</EmploymentNo>
    <Active>str1234</Active>
    <ActiveStart>str1234</ActiveStart>
    <ActiveEnd>str1234</ActiveEnd>
    <DeliveryAddress>str1234</DeliveryAddress>
    <DeliveryPostalCode>str1234</DeliveryPostalCode>
    <DeliveryCity>str1234</DeliveryCity>
    <DeliveryAddressNoteText>str1234</DeliveryAddressNoteText>
    <AccountRef1>str1234</AccountRef1>
    <AccountRef2>str1234</AccountRef2>
    <AccountRef3>str1234</AccountRef3>
    <AccountRef4>str1234</AccountRef4>
    <RegisteredDate>str1234</RegisteredDate>
    <Closed>str1234</Closed>
  </Project>
</SymbrioXMLProjectIn>

ProjectInRequest - 节点不存在。

我可能遗漏了什么,请指出。

提前致谢。

使用xpath提取你想要的记录

//*[local-name()='SymbrioXMLProjectIn']

nvm找到解决办法了,我XmlSeralized错了,正确的方法应该是

XmlSerializer ser = new XmlSerializer(request.SymbrioXMLProjectIn.GetType()); 

糟糕的方法是

 XmlSerializer ser = new XmlSerializer(request.GetType());

如果有人像我一样犯了错误,我会把它留在这里。