我试图在 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">