0

我试图在 XML 文档中获取文件名的最后一部分。目前,一些用户正在发送完整的文件路径,但不是全部。我知道在处理物理文件时如何进行修剪,但当路径包含在 XML 的“nc:DocumentFileControlID”元素内时,该元素位于“ecf:DocumentRendition”元素下。我想要最后一个“\”之后和 .pdf 或 .doc 文件扩展名之前的路径。

C#代码:

private void btnGet_File_Click(object sender, System.EventArgs e)
{
    OpenFileDialog1.Title = "Please Select a File";
    OpenFileDialog1.InitialDirectory = @"filepath";
    OpenFileDialog1.FileName = TextBox1.Text;
    OpenFileDialog1.Filter = TextBox1.Text + "|*.xml";

    OpenFileDialog1.ShowDialog();
    string b64str = null;
    byte[] binaryData = null;
    System.IO.FileStream fs = null;
    XmlReader xr = XmlReader.Create(fileToOpen);

    ListBox1.Items.Clear();

    while (xr.Read())
    {
        if (xr.NodeType == XmlNodeType.Element && xr.Name == "nc:DocumentFileControlID")
        {
            fileToSave = xr.ReadElementString();
            if (fileToSave.Contains(".pdf") || fileToSave.Contains(".doc"))
            {
                ListBox1.Items.Add(fileToSave);
            }
        }
        if (xr.NodeType == XmlNodeType.Element && xr.Name == "nc:BinaryBase64Object")
        {
            b64str = xr.ReadElementString();
            binaryData = Convert.FromBase64String(b64str);

            fileToSave = "C:\\xml_images\\" + fileToSave;
            fs = new FileStream(fileToSave, FileMode.Create);

            // write it out
            fs.Write(binaryData, 0, binaryData.Length);

            // close it down.
            fs.Close();
            binaryData = null;
            fileToSave = null;
        }
    }
    MessageBox.Show("All files are saved in C:\\xml_images");
}

XML 文件:

<?xml version="1.0"?>
<RecordFilingRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:oasis:names:tc:legalxml-courtfiling:wsdl:WebServiceMessagingProfile-Definitions-4.0">
  <RecordFilingRequestMessage xmlns:fsrsp="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:FilingStatusResponseMessage-4.0" xmlns:ecf="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CommonTypes-4.0" xmlns:j="http://niem.gov/niem/domains/jxdm/4.0" xmlns:juvenile="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:JuvenileCase-4.0" xmlns:niem-xsd="http://niem.gov/niem/proxy/xsd/2.0" xmlns:domestic="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:DomesticCase-4.0" xmlns:s="http://niem.gov/niem/structures/2.0" xmlns:criminal="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CriminalCase-4.0" xmlns:amcadext="http://www.amcad.com/NiemEcf/extensions/1.0" xmlns:i="http://niem.gov/niem/appinfo/2.0" xmlns:appellate="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:AppellateCase-4.0" xmlns:nc="http://niem.gov/niem/niem-core/2.0" xmlns:citation="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CitationCase-4.0" xmlns:reviewcb="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:ReviewFilingCallbackMessage-4.0" xmlns:civil="urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CivilCase-4.0">

  <FilingLeadDocument s:id="DOC00001" s:metadata="# Pages=4">
    <nc:DocumentApplicationName>application/pdf</nc:DocumentApplicationName>
    <nc:DocumentDescriptionText s:id="Service Documents">Alias Summons Returned Served</nc:DocumentDescriptionText>
    <nc:DocumentFileControlID s:id="FileInputId">101</nc:DocumentFileControlID>
    <nc:DocumentFileControlID s:id="Rule6PublicAnswer">-1</nc:DocumentFileControlID>
    <nc:DocumentFileControlID s:id="Rule6ConfidentialAnswer">-1</nc:DocumentFileControlID>
    <nc:DocumentFileControlID s:id="TypeOfConfidentialDocument">-1</nc:DocumentFileControlID>
    <nc:DocumentPostDate>
      <nc:DateTime>2013-10-02T09:57:49.4565724-04:00</nc:DateTime>
    </nc:DocumentPostDate>
    <nc:DocumentReceivedDate>
      <nc:DateTime>2013-10-02T09:57:48.463-04:00</nc:DateTime>
    </nc:DocumentReceivedDate>
    <nc:DocumentSequenceID>1</nc:DocumentSequenceID>
    <ecf:DocumentMetadata>
      <j:RegisterActionDescriptionText s:id="2090267461">7645</j:RegisterActionDescriptionText>
      <ecf:FilingPartyID>
        <nc:IdentificationID>17291</nc:IdentificationID>
        <nc:IdentificationCategoryText>FLEPORTAL</nc:IdentificationCategoryText>
      </ecf:FilingPartyID>
      <ecf:SpecialHandlingInstructions />
    </ecf:DocumentMetadata>
    <ecf:DocumentRendition>
      <ecf:DocumentRenditionMetadata>
        <nc:DocumentApplicationName>application/pdf</nc:DocumentApplicationName>
        <nc:DocumentFileControlID>H:\`BCA E-Filing Folder\EFRAIN\13-256382_FC01_SUMMONS_BREVARD1.pdf</nc:DocumentFileControlID>
        <ecf:DocumentAttachment s:id="ATT00001">
4

2 回答 2

1

利用

System.IO.Path.GetFileNameWithoutExtension("H:\BCA E-Filing Folder\EFRAIN\13-256382_FC01_SUMMONS_BREVARD1.pdf");

那会得到

13-256382_FC01_SUMMONS_BREVARD1

于 2013-10-31T17:43:43.020 回答
1

更新

您需要先从元素节点的值中提取数据。我已经获取了您的 xml 并使用 Linq to Xml 对其进行了解析并提取了该值。请注意,有许多 DocumentFileControlID,我只是寻找具有 ac: 或 h: 类型模式的一个来区分。您的处理方式会有所不同。

    XNamespace nc = "http://niem.gov/niem/niem-core/2.0";
    
    var node =
    XElement.Parse(GetXMlData())
            .Descendants(nc + "DocumentFileControlID")
            .Where (element => Regex.IsMatch(element.Value, @"(^[A-Z]\:)"))
        //  .Select (element => Path.GetFileName(element.Value)) // Use this to just get the filename.
            .FirstOrDefault();
    
    Console.WriteLine (node); // <nc:DocumentFileControlID xmlns:nc="http://niem.gov/niem/niem-core/2.0">H:\`BCA E-Filing Folder\EFRAIN\13-256382_FC01_SUMMONS_BREVARD1.pdf</nc:DocumentFileControlID>
    
    Console.WriteLine (Path.GetFileName(node.Value));   
//  13-256382_FC01_SUMMONS_BREVARD1.pdf

GetData 看起来像这样:

public string GetXMlData()
{
    return "<?xml version=\"1.0\"?>\r\n<RecordFilingRequest xmlns:xsi=\"http://www.w3.org/2001/XML" +
    "Schema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"urn:oasis:n" +
    "ames:tc:legalxml-courtfiling:wsdl:WebServiceMessagingProfile-Definitions-4.0\">\r\n" +
    "  <RecordFilingRequestMessage xmlns:fsrsp=\"urn:oasis:names:tc:legalxml-courtfili" +
    "ng:schema:xsd:FilingStatusResponseMessage-4.0\" xmlns:ecf=\"urn:oasis:names:tc:leg" +
    "alxml-courtfiling:schema:xsd:CommonTypes-4.0\" xmlns:j=\"http://niem.gov/niem/doma" +
    "ins/jxdm/4.0\" xmlns:juvenile=\"urn:oasis:names:tc:legalxml-courtfiling:schema:xsd" +
    ":JuvenileCase-4.0\" xmlns:niem-xsd=\"http://niem.gov/niem/proxy/xsd/2.0\" xmlns:dom" +
    "estic=\"urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:DomesticCase-4.0\" xmln" +
    "s:s=\"http://niem.gov/niem/structures/2.0\" xmlns:criminal=\"urn:oasis:names:tc:leg" +
    "alxml-courtfiling:schema:xsd:CriminalCase-4.0\" xmlns:amcadext=\"http://www.amcad." +
    "com/NiemEcf/extensions/1.0\" xmlns:i=\"http://niem.gov/niem/appinfo/2.0\" xmlns:app" +
    "ellate=\"urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:AppellateCase-4.0\" xm" +
    "lns:nc=\"http://niem.gov/niem/niem-core/2.0\" xmlns:citation=\"urn:oasis:names:tc:l" +
    "egalxml-courtfiling:schema:xsd:CitationCase-4.0\" xmlns:reviewcb=\"urn:oasis:names" +
    ":tc:legalxml-courtfiling:schema:xsd:ReviewFilingCallbackMessage-4.0\" xmlns:civil" +
    "=\"urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CivilCase-4.0\">\r\n\r\n  <Filin" +
    "gLeadDocument s:id=\"DOC00001\" s:metadata=\"# Pages=4\">\r\n    <nc:DocumentApplicati" +
    "onName>application/pdf</nc:DocumentApplicationName>\r\n    <nc:DocumentDescription" +
    "Text s:id=\"Service Documents\">Alias Summons Returned Served</nc:DocumentDescript" +
    "ionText>\r\n    <nc:DocumentFileControlID s:id=\"FileInputId\">101</nc:DocumentFileC" +
    "ontrolID>\r\n    <nc:DocumentFileControlID s:id=\"Rule6PublicAnswer\">-1</nc:Documen" +
    "tFileControlID>\r\n    <nc:DocumentFileControlID s:id=\"Rule6ConfidentialAnswer\">-1" +
    "</nc:DocumentFileControlID>\r\n    <nc:DocumentFileControlID s:id=\"TypeOfConfident" +
    "ialDocument\">-1</nc:DocumentFileControlID>\r\n    <nc:DocumentPostDate>\r\n      <nc" +
    ":DateTime>2013-10-02T09:57:49.4565724-04:00</nc:DateTime>\r\n    </nc:DocumentPost" +
    "Date>\r\n    <nc:DocumentReceivedDate>\r\n      <nc:DateTime>2013-10-02T09:57:48.463" +
    "-04:00</nc:DateTime>\r\n    </nc:DocumentReceivedDate>\r\n    <nc:DocumentSequenceID" +
    ">1</nc:DocumentSequenceID>\r\n    <ecf:DocumentMetadata>\r\n      <j:RegisterActionD" +
    "escriptionText s:id=\"2090267461\">7645</j:RegisterActionDescriptionText>\r\n      <" +
    "ecf:FilingPartyID>\r\n        <nc:IdentificationID>17291</nc:IdentificationID>\r\n  " +
    "      <nc:IdentificationCategoryText>FLEPORTAL</nc:IdentificationCategoryText>\r\n" +
    "      </ecf:FilingPartyID>\r\n      <ecf:SpecialHandlingInstructions />\r\n    </ecf" +
    ":DocumentMetadata>\r\n    <ecf:DocumentRendition>\r\n      <ecf:DocumentRenditionMet" +
    "adata>\r\n        <nc:DocumentApplicationName>application/pdf</nc:DocumentApplicat" +
    "ionName>\r\n        <nc:DocumentFileControlID>H:\\`BCA E-Filing Folder\\EFRAIN\\13-25" +
    "6382_FC01_SUMMONS_BREVARD1.pdf</nc:DocumentFileControlID>\r\n        <ecf:Document" +
    "Attachment s:id=\"ATT00001\"/>\r\n\t   </ecf:DocumentRenditionMetadata>\r\n\t</ecf:Docum" +
    "entRendition>\r\n</FilingLeadDocument>\r\n</RecordFilingRequestMessage>\r\n</RecordFil" +
    "ingRequest>";

}

原来的

使用 Path.GetFileName 将确定文件是否有路径并返回文件名。

Path.GetFileName(@"H:\`BCA E-Filing Folder\EFRAIN\13-256382_FC01_SUMMONS_BREVARD1.pdf")
    
    /* returns
     13-256382_FC01_SUMMONS_BREVARD1.pdf
    */
于 2013-10-31T17:44:51.463 回答